ICSE Class 10 Computer Applications
Question 17 of 46
String Handling — Question 17
Back to all questions 17
Question Question 4
Write a program to input a sentence and print each word of the string along with its length in tabular form.
import java.util.Scanner;
public class KboatWordsLengthTable
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
str += " ";
int len = str.length();
String word = "";
int wLen = 0;
System.out.println("Word Length");
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch != ' ') {
word = word + ch;
wLen++;
}
else {
System.out.println(word + "\t" + wLen);
word = "";
wLen = 0;
}
}
}
}Output
