Rotate String in Java

A class Rotate is designed to handle string related operations. Some of the members of the class are given below:
Class name: Rotate
Data members:
txt: to store the given string.
Methods:
Rotate(): constructor
void readStr(): to accept the string.
void title(): to convert the first letter of every word to uppercase.
void circular(): to decode the original string by replacing each letter by converting it to opposite case and then by the next character in a circular way.

Example: “AbZ cDy” will be decoded as “bCa deZ”.

Specify the class giving the details of the methods and also include main().

Program:

import java.io.*;
class Rotate{
    String txt;
    public Rotate(){
        txt = new String();
    }
    public void readStr()throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Enter the string: ");
        txt = br.readLine();
    }
    public void title(){
        String t = new String();
        for(int i = 0; i < txt.length(); i++){
            char ch = txt.charAt(i);
            if(i == 0 || txt.charAt(i - 1) == ' ')
                t += Character.toUpperCase(ch);
            else
                t += ch;
        }
        txt = t;
    }
    public void circular(){
        String t = new String();
        for(int i = 0; i < txt.length(); i++){
            char ch = txt.charAt(i);
            if(!Character.isLetter(ch))
                t += ch;
            else if(ch == 'z')
                t += 'A';
            else if(ch == 'Z')
                t += 'a';
            else if(Character.isUpperCase(ch))
                t += (char)(Character.toLowerCase(ch) + 1);
            else
                t += (char)(Character.toUpperCase(ch) + 1);
        }
        txt = t;
        System.out.println(txt);
    }
    public static void main(String args[])
    throws IOException{
        Rotate obj = new Rotate();
        obj.readStr();
        obj.title();
        obj.circular();
    }
}

Leave a Reply