Class 11 Class 12

TheString Program ISC 2015

A class TheString accepts a string of a maximum of 100 characters with only one blank space between the words.

Some of the members of the class are as follows:

Class name: TheString
Data members/instance variables:
str: to store a string.
len: integer to store the length of the string.
wordCount: integer to store the number of words.
cons: integer to store the number of consonants.
Member functions/methods:
TheString(): default constructor to initialize the data members.
The String(String ds): parameterized constructor to assign str = ds.
void countFreq(): to count the number of words and the number of consonants and store them in wordCount and cons respectively.
void display(): to display the original string, along with the number of words and the number of consonants.

Specify the class TheString giving the details of the constructors, void countFreq() and display(). Define the main() function to create an object and call the functions accordingly to enable the task.

class TheString{
    private String str;
    private int len;
    private int wordCount;
    private int cons;
    public TheString(){
        str = new String();
        len = 0;
        wordCount = 0;
        cons = 0;
    public TheString(String ds){
        str = ds;
        if(str.length() > 100)
            str = str.substring(0, 100);
        str = str.trim();
        len = str.length();
        wordCount = 0;
        cons = 0;
    public void countFreq(){
        if(len > 0)
            wordCount = 1;
        String s = str.replace(" ", "");
        wordCount += len - s.length();
        s = new String(str);
        for(int i = 0; i < len; i++){
            char ch = str.charAt(i);
            ch = Character.toUpperCase(ch);
                    case 'A':
                    case 'E':
                    case 'I':
                    case 'O':
                    case 'U':
    public void display(){
        System.out.println("Original string: " + str);
        System.out.println("Number of words: " + wordCount);
        System.out.println("Number of consonants: " + cons);
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(;
        BufferedReader br = new BufferedReader(in);
        System.out.print("Enter the string: ");
        String s = br.readLine();
        TheString obj = new TheString(s);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.