Display each word with no of letter in it (Program 4) by School
import java.util.*;
public class Program_4 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter Sentence which end with ? . !");
String sentence = in.nextLine();
if (sentence.endsWith(".") || sentence.endsWith("!") || sentence.endsWith("?")) {
sentence = sentence.substring(0, sentence.length() - 1);
StringTokenizer st = new StringTokenizer(sentence);
int length = 0;
String word = "";
while (st.hasMoreTokens()) {
word = st.nextToken();
length = word.length();
System.out.println(word + "\t" + length);
}
} else {
System.out.println("Invalid Input");
}
}
}
Comments
Post a Comment