ISC Practicals and Theory - Word Extraction and Count
Updated 21 Oct 2026
This program reads a sentence, picks out each word one by one using only charAt(), substring() and length(), prints every word on its own line with its length, and prints the number of words. It does not use StringTokenizer or split(). Everything is written in the main method.
Definition
A word here is a run of characters between spaces. The sentence must end with ., ? or !; if it does not, the program prints INVALID INPUT. The decisions the program makes are:
- The terminating character (
.,?or!) is not part of the last word. InHello.the word isHello, of length 5. - Several spaces in a row, and spaces at the start of the sentence, are skipped. They never produce an empty word.
- A space before the terminating character (as in
Is it raining ?) is fine;?alone is not counted as a word. - Spaces at the end of the line are ignored too (the program trims the line), so
Hello.is valid. - A sentence with only the terminating character, such as
., has no word at all, so it isINVALID INPUT.
Algorithm
- Read the sentence into
sand remove the spaces at its two ends withtrim(). - The sentence is valid only if it has at least 2 characters and its last character is
.,?or!. If not, printINVALID INPUTand stop. - Set
start = -1(no word is open) andcount = 0. For every indexiof the sentence:- if the character is a space, or
iis the last index (the terminator), it is a separator: if a word is open (start != -1), the word iss.substring(start, i); add 1 tocount, print the word with its length and setstart = -1; - otherwise, if no word is open, this is the first letter of a new word, so set
start = i.
- if the character is a space, or
- After the loop print the number of words.
Program
The single for loop in main opens a word at its first letter and closes it at the next space or at the terminator.
import java.util.Scanner;
/* Program Words reads a sentence, extracts its words without using StringTokenizer
or split(), prints each word with its length and prints the word count. */
public class Words
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String s = sc.nextLine().trim(); // trim() removes the spaces at the two ends
int len = s.length(); // length of the sentence
boolean valid = false; // true if there is a word and a terminator . ? or !
if (len >= 2) // empty, or only a terminator, is invalid
{
char last = s.charAt(len - 1); // terminating character
valid = last == '.' || last == '?' || last == '!';
}
if (!valid)
{
System.out.println("INVALID INPUT");
return;
}
int start = -1; // index where the current word begins, -1 if none
int count = 0; // number of words found
for (int i = 0; i < len; i++)
{
char ch = s.charAt(i); // character being examined
if (ch == ' ' || i == len - 1) // separator: a word may have just ended
{
if (start != -1)
{
String w = s.substring(start, i); // the word found
count++;
System.out.println(w + " (" + w.length() + ")");
start = -1;
}
}
else if (start == -1) // first letter of a new word
start = i;
}
System.out.println("Number of words = " + count);
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
s | String | The sentence typed by the user, after trim() |
len | int | Length of the sentence |
valid | boolean | True if the sentence has a word and ends with ., ? or ! |
last | char | The last character of the sentence, the terminator |
start | int | Index where the current word begins, -1 if no word is open |
count | int | Number of words found |
i | int | Loop counter over the characters |
ch | char | The character being examined |
w | String | The word found, printed with its length |
Sample input and output
Sample 1
Enter a sentence: The sun rises in the east!
The (3)
sun (3)
rises (5)
in (2)
the (3)
east (4)
Number of words = 6Sample 2
Enter a sentence: Hello.
Hello (5)
Number of words = 1Sample 3
Enter a sentence: Is it raining ?
Is (2)
it (2)
raining (7)
Number of words = 3Sample 4
Enter a sentence: Good morning
INVALID INPUTSample 5
Enter a sentence: .
INVALID INPUTSample 6
Enter a sentence: Good evening.
Good (4)
evening (7)
Number of words = 2Dry run
Take the third sample, Is it raining ?. The characters are at indices: I0 s1 space2 i3 t4 space5 r6 a7 i8 n9 i10 n11 g12 space13 ?14, so len = 15.
| i | ch | start before | Action | w | count after |
|---|---|---|---|---|---|
| 0 | I | -1 | start = 0 | 0 | |
| 1 | s | 0 | none | 0 | |
| 2 | space | 0 | word ends | Is | 1 |
| 3 | i | -1 | start = 3 | 1 | |
| 4 | t | 3 | none | 1 | |
| 5 | space | 3 | word ends | it | 2 |
| 6 | r | -1 | start = 6 | 2 | |
| 7 to 12 | a i n i n g | 6 | none | 2 | |
| 13 | space | 6 | word ends | raining | 3 |
| 14 | ? | -1 | last index: separator, but no word open | 3 |
The program prints Is (2), it (2), raining (7) and Number of words = 3, the same as the real run above.
Exam tip: Use substring(start, i): its end index is excluded, so the space or terminating character is left out of the word.
Variations
- Longest word "Accept a sentence and display the longest word and its length."
- Reverse each word "Accept a sentence and display it with every word reversed, keeping the order of the words."
- Words starting with a vowel "Accept a sentence and count the words that begin with a vowel."
- Reverse word order "Accept a sentence and display its words in reverse order."