Here’s a simple Java program that takes a sentence as input and prints the number of letters in each word:
β Java Program: Count Letters in Each Word
import java.util.Scanner;
public class WordLengthCounter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input sentence
System.out.print(“Enter a sentence: “);
String sentence = scanner.nextLine();
// Split the sentence into words using space
String[] words = sentence.split(” “);
// Loop through each word and print its length
for (String word : words) {
System.out.println(word + “=” + word.length());
}
}
}
β Sample Input:
hello world java code
β Output:
hello=5
world=5
java=4
code=4
. Write a java program that takes a sentence as input and prints numbers of letters in each word
input: “hello world java code”
output:
hello=5
world=5
java=4
code=4
class Find
{
public static void main(String args[])
{
String data=”hello world java code”;
String another[]=data.split(” “);{hello–>0
world–>1
java–>2
code–>3}–>another
for(String a:another)
{
System.out.println(a+”=”+a.length());//hello=5 world=5 java=4 code=4
}}}