Decoding String using Circular Decode

A class StringOp is designed to handle string-related operations.

Some members of the class are given below:
Data member:
txt: to store the given string of maximum length 100.
Member functions:
StringOp(): constructor.
void readString(): to accept the string.
char caseConvert(char): to convert the letter to other case.
void circularDecode(): to decode the string by replacing each letter by converting it to opposite case and then by the next character in a circular way. Hence “AbZ” will decode to “bCa”.

Specify the class giving details of all the member functions. You need to write function main().

import java.io.*;
class StringOp{
    String txt;
    public StringOp(){
        txt = "";
    }
    public void readString()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the string: ");
        txt = br.readLine();
        if(txt.length() > 100)
            txt = txt.substring(0, 100);
    }
    public char caseConvert(char ch){
        if(Character.isUpperCase(ch))
            return Character.toLowerCase(ch);
        return Character.toUpperCase(ch);
    }
    public void circularDecode(){
        String s = "";
        for(int i = 0; i < txt.length(); i++){
            char ch = txt.charAt(i);
            ch = caseConvert(ch);
            if(ch == 'Z')
                s += 'A';
            else if(ch == 'z')
                s += 'a';
            else if(Character.isLetter(ch))
                s += (char)(ch + 1);
            else
                s += ch;
        }
        System.out.println(s);
    }
    public static void main(String args[])throws IOException{
        StringOp obj = new StringOp();
        obj.readString();
        obj.circularDecode();
    }
}

Leave a Reply

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