Change Case of Individual Letters in a Word

Design a class Change to perform string related operations. The details of the class are given below:

Class name: Change
Data members:
str: stores the word.
newStr: stores the changed word.
len: stores the length of the word.
Member functions:
Change(): default constructor.
void inputWord(): to accept a word.
char caseConvert(char ch): converts the case of the character and returns it.
void recChange(int): extracts characters using recursive technique and changes its case using caseConvert() and forms a new word.
void display(): displays both the words.

Specify the class Change, giving details of the constructor, member functions void inputWord(), char caseConvert(char ch), void recChange(int) and void display().

Define the main() function to create an object and call the functions accordingly to enable the above change in the given word.

import java.io.*;
class Change{
    String str;
    String newStr;
    int len;
    public Change(){
        str = "";
        newStr = "";
        len = 0;
    }
    public void inputWord()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Word: ");
        str = br.readLine();
        str = str.trim();
        if(str.indexOf(' ') > 0)
            str = str.substring(0, str.indexOf(' '));
        len = str.length();
    }
    public char caseConvert(char ch){
        if(Character.isUpperCase(ch))
            return Character.toLowerCase(ch);
        return Character.toUpperCase(ch);
    }
    public void recChange(int i){
        if(i == len)
            return;
        newStr += caseConvert(str.charAt(i));
        recChange(i + 1);
    }
    public void display(){
        System.out.println("Original word: " + str);
        System.out.println("Changed word: " + newStr);
    }
    public static void main(String args[])throws IOException{
        Change obj = new Change();
        obj.inputWord();
        obj.recChange(0);
        obj.display();
    }
}

Leave a Reply

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