Categories
Class 10 Class 11 Class 12

Right Rotation of a Word in Java

Design a class Rotate using the description of the data member and member functions given below:

Class name: Rotate
Data members:
str: to store a sentence.
Member functions:
Rotate(): constructor to initialize the instance variable.
void readStr(): to input the sentence in uppercase.
void display(): convert each word of the sentence in right rotation and display all the right rotation of every word.
Example: Right rotation of the word SKY is: SKY, KYS, YSK.

import java.io.*;
import java.util.StringTokenizer;
class Rotate{
    String str;
    public void readStr()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the sentence: ");
        str = br.readLine();
        str = str.toUpperCase();
    }
    public void display(){
        StringTokenizer st = new StringTokenizer(str, " ?.!,");
        int count = st.countTokens();
        for(int i = 1; i <= count; i++){
            String word = st.nextToken();
            for(int j = 0; j < word.length(); j++){
                System.out.print(word + "\t");
                word = word.substring(1) + word.charAt(0);
            }
            System.out.println();
        }
    }
    public static void main(String args[])throws IOException{
        Rotate obj = new Rotate();
        obj.readStr();
        obj.display();
    }
}

By admin

I am a Computer Science teacher in one of the renowned schools in India. I teach Java programming language to my students, and I maintain a website happycompiler.com

Leave a Reply

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