ConsChange Program ISC 2016

A class ConsChange has been defined with the following details:

Class name: ConsChange
Data members/instance variables:
word: stores the word.
len: stores the length of the word.
Member functions/methods:
ConsChange(): default constructor
void readWord(): accepts the word in lowercase.
void shiftCons(): shifts all the consonants of the word at the beginning followed by the vowels (e.g. spoon becomes spnoo).
void changeWord(): changes the case of all occurring consonants of the shifted word to uppercase, for e.g. spnoo becomes SPNoo)
void display(): displays the original word, shifted word and the changed word.

Specify the class ConsChange giving the details of the constructor, void readWord(), void shiftCons(), void changeWord() and void show(). Define the main() function to create an object and call the functions accordingly to enable the task.

import java.io.*;
class ConsChange{
    private String word;
    private int len;
    public ConsChange(){
        word = new String();
        len = 0;
    }
    public void readWord()throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Word: ");
        word = br.readLine().toLowerCase();
        word = word.trim();
        if(word.indexOf(' ') > 0)
            word = word.substring(0, word.indexOf(' '));
        len = word.length();
    }
    public void shiftCons(){
        String c = new String();
        String v = new String();
        for(int i = 0; i < len; i++){
            char ch = word.charAt(i);
            switch(ch){
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                v += ch;
                break;
                default:
                c += ch;
            }
        }
        word = c + v;
        System.out.println("Shifted word: " + word);
    }
    public void changeWord(){
        int pos = 0;
        loop:
        for(int i = 0; i < len; i++){
            char ch = word.charAt(i);
            switch(ch){
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                pos = i;
                break loop;
            }
        }
        String c = word.substring(0, pos);
        c = c.toUpperCase();
        String v = word.substring(pos);
        word = c + v;
        System.out.println("Changed word: " + word);
    }
    public void show(){
        System.out.println("Original word: " + word);
        this.shiftCons();
        this.changeWord();
    }
    public static void main(String args[])
    throws IOException{
        ConsChange obj = new ConsChange();
        obj.readWord();
        obj.show();
    }
}

2 thoughts on “ConsChange Program ISC 2016

  1. Sir,a program is given which states that:
    WAP in Java to accept 2 strings.Now create a new string which is created by the combination of the above 2 strings taking 1 character alternately from those 2 strings followed by all the remaining characters of the longer string if any.
    Sample Input:
    First String: WALNUT
    Second String:TOP
    Sample Output:
    Resultant String:WTAOLPNUT

Leave a Reply

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