ISC Practicals and Theory - Sentence Terminator Validation
Updated 21 Oct 2026
This program reads a sentence and checks that it is properly ended. A sentence is accepted only if it finishes with a full stop, a question mark or an exclamation mark. For a valid sentence the program prints the terminator, the kind of sentence it suggests and the number of words; otherwise it prints INVALID INPUT. The words are counted with charAt() and length() only, with no StringTokenizer or split(). Everything is written in the main method.
Definition
A terminator is the last character of a sentence: ., ? or !. It suggests the kind of sentence:
.: assertive (declarative) sentence, for exampleIndia is my country.?: interrogative sentence, for exampleHow are you?!: exclamatory sentence, for exampleWow!
A word is a run of characters without spaces. A new word begins at a non-space character that is either the first character of the string or comes just after a space.
The program follows these rules for the awkward inputs:
- Leading spaces are skipped naturally, and trailing spaces after the terminator are ignored.
How are you ?is valid and ends with?. - An empty line (or a line of only spaces) is
INVALID INPUT. - A terminator only in the middle, such as
Hello. How are you, isINVALID INPUT, because the last character is not a terminator. - A line that is only the terminator, such as
?, isINVALID INPUT, because there is no word before it. - The terminator is not counted as a word, even when it is separated by a space (
How are you ?has 3 words). Words are separated only by spaces, soStop.Look!counts as 1 word.
Algorithm
- Read the line into
str. - Find
last, the index of the last character that is not a space: start atstr.length() - 1and move left while the character is a space. Trailing spaces are therefore ignored. Iflastis -1, the line is empty or has only spaces, and it is invalid. - Take
term = str.charAt(last). If it is not.,?or!, the sentence is invalid. - Count the words in
words, examining the characters up to indexlast - 1: a word begins at a character that is not a space and is either the first character or follows a space. - The sentence is valid only if
words > 0, because a lone terminator is not a sentence. - For a valid sentence, the kind depends on the terminator:
.is Assertive,?is Interrogative and!is Exclamatory. Print the terminator, the kind and the number of words. Otherwise printINVALID INPUT.
Program
The while loop in main finds last, the for loop counts the words, and the if ladder decides the kind of sentence.
import java.util.Scanner;
/* Program Sentence checks whether a sentence ends with a valid terminator
(. ? !) and, if so, reports the terminator, the kind of sentence and
the number of words. */
public class Sentence
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String str = sc.nextLine(); // the sentence entered by the user
int last = str.length() - 1; // index of the last character that is not a space (-1 if none)
while (last >= 0 && str.charAt(last) == ' ') // skip trailing spaces
last--;
boolean valid = false; // true for a proper sentence
char term = ' '; // the terminator found at the end of the sentence
int words = 0; // the number of words in the sentence
if (last >= 0) // not an empty line or only spaces
{
term = str.charAt(last);
if (term == '.' || term == '?' || term == '!')
{
// a word begins at a non-space character that is the first character or follows a space;
// the characters up to index last - 1 are examined
for (int i = 0; i < last; i++)
{
if (str.charAt(i) != ' ' && (i == 0 || str.charAt(i - 1) == ' '))
words++;
}
valid = words > 0; // a lone terminator is not a sentence
}
}
if (valid)
{
String kind = "Exclamatory"; // the kind of sentence
if (term == '.')
kind = "Assertive";
else if (term == '?')
kind = "Interrogative";
System.out.println("Terminator: " + term);
System.out.println("Kind of sentence: " + kind);
System.out.println("Number of words: " + words);
}
else
System.out.println("INVALID INPUT");
}
}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 |
str | String | The sentence typed by the user |
last | int | Index of the last character that is not a space, -1 if there is none |
valid | boolean | True if the line is a proper sentence |
term | char | The terminator found at the end of the sentence |
words | int | Number of words in the sentence |
i | int | Loop counter over the characters before the terminator |
kind | String | The kind of sentence: Assertive, Interrogative or Exclamatory |
Sample input and output
Sample 1
Enter a sentence: India is my country.
Terminator: .
Kind of sentence: Assertive
Number of words: 4Sample 2
Enter a sentence: How are you ?
Terminator: ?
Kind of sentence: Interrogative
Number of words: 3Sample 3
Enter a sentence: Wow!
Terminator: !
Kind of sentence: Exclamatory
Number of words: 1Sample 4
Enter a sentence: Hello. How are you
INVALID INPUTSample 5
Enter a sentence: ?
INVALID INPUTDry run
Input India is my country. (20 characters). There is no trailing space, so last = 19 and term = ., which is valid. The word count now examines indices 0 to 18 only.
i | str.charAt(i) | Non-space and (i = 0 or space before)? | words |
|---|---|---|---|
| 0 | I | yes, first character | 1 |
| 1 to 4 | n, d, i, a | no, letter before | 1 |
| 5 | space | no, it is a space | 1 |
| 6 | i | yes, space before | 2 |
| 7 | s | no | 2 |
| 8 | space | no | 2 |
| 9 | m | yes, space before | 3 |
| 10 | y | no | 3 |
| 11 | space | no | 3 |
| 12 | c | yes, space before | 4 |
| 13 to 18 | o, u, n, t, r, y | no | 4 |
The loop stops at i = 19, so words = 4. The terminator . means Assertive, and the program prints Terminator: ., Kind of sentence: Assertive and Number of words: 4, the same as the real run above.
Variations
- Count sentences "Write a program to accept a paragraph and count the number of sentences ending with . ? or !"
- Longest word "Write a program to accept a sentence and display its longest word."
- Capitalise words "Write a program to accept a sentence and convert the first letter of every word to capital."