Shift Consonants Java Program | ISC Computer Science 2016 Theory

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 show(): 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.util.Scanner;
class ConsChange{
    String word;
    int len;
    public ConsChange(){
        word = new String();
        len = 0;
    }
    public void readWord(){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the word: ");
        word = in.next().toLowerCase();
        len = word.length();
    }
    public void shiftCons(){
        String v = new String();
        String c = new String();
        for(int i = 0; i < len; i++){
            char ch = word.charAt(i);
            if("aeiou".indexOf(ch) >= 0)
                v += ch;
            else
                c += ch;
        }
        word = c + v;
    }
    public void changeWord(){
        int i = 0;
        while(i < len){
            char ch = word.charAt(i);
            if("aeiou".indexOf(ch) >= 0)
                break;
            i++;
        }
        String c = word.substring(0, i).toUpperCase();
        String v = word.substring(i);
        word = c + v;
    }
    public void show(){
        System.out.println("Original word: " + word);
        shiftCons();
        System.out.println("Shifted word: " + word);
        changeWord();
        System.out.println("Changed word: " + word);
    }
    public static void main(String[] args){
        ConsChange obj = new ConsChange();
        obj.readWord();
        obj.show();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *