Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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 example India is my country.
  • ? : interrogative sentence, for example How are you?
  • ! : exclamatory sentence, for example Wow!

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, is INVALID INPUT, because the last character is not a terminator.
  • A line that is only the terminator, such as ?, is INVALID 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, so Stop.Look! counts as 1 word.

Algorithm

  1. Read the line into str.
  2. Find last, the index of the last character that is not a space: start at str.length() - 1 and move left while the character is a space. Trailing spaces are therefore ignored. If last is -1, the line is empty or has only spaces, and it is invalid.
  3. Take term = str.charAt(last). If it is not ., ? or !, the sentence is invalid.
  4. Count the words in words, examining the characters up to index last - 1: a word begins at a character that is not a space and is either the first character or follows a space.
  5. The sentence is valid only if words > 0, because a lone terminator is not a sentence.
  6. 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 print INVALID 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

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the input typed by the user
strStringThe sentence typed by the user
lastintIndex of the last character that is not a space, -1 if there is none
validbooleanTrue if the line is a proper sentence
termcharThe terminator found at the end of the sentence
wordsintNumber of words in the sentence
iintLoop counter over the characters before the terminator
kindStringThe 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: 4

Sample 2

Enter a sentence:    How   are  you ?   
Terminator: ?
Kind of sentence: Interrogative
Number of words: 3

Sample 3

Enter a sentence: Wow!
Terminator: !
Kind of sentence: Exclamatory
Number of words: 1

Sample 4

Enter a sentence: Hello. How are you
INVALID INPUT

Sample 5

Enter a sentence: ?
INVALID INPUT

One value per line

Dry 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.

istr.charAt(i)Non-space and (i = 0 or space before)?words
0Iyes, first character1
1 to 4n, d, i, ano, letter before1
5spaceno, it is a space1
6iyes, space before2
7sno2
8spaceno2
9myes, space before3
10yno3
11spaceno3
12cyes, space before4
13 to 18o, u, n, t, r, yno4

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

  1. Count sentences "Write a program to accept a paragraph and count the number of sentences ending with . ? or !"
  2. Longest word "Write a program to accept a sentence and display its longest word."
  3. Capitalise words "Write a program to accept a sentence and convert the first letter of every word to capital."