Software & Finance





Java - String Rotate





Here is the Java Source code to rotate the given string.

 

Source Code


import java.io.*;

 

 

class StringRotate {   

 

    public static void main(String[] args) {

 

            String inpstring = "";

            InputStreamReader input = new InputStreamReader(System.in);

            BufferedReader reader = new BufferedReader(input);

 

            try

            {

                  System.out.println("Enter a string to rotate:");

                  inpstring = reader.readLine();

 

                  int len = inpstring.length();

                  int lastindex = len - 1;

 

                  char[] outstring = inpstring.toCharArray();

 

 

                  for (int i = 0; i < len; i++)

                  {

                        char ch = outstring[0];

                        for (int j = 0; j < len - 1; j++)

                        {

                              outstring[j] = outstring[j + 1];

                        }

                        outstring[len - 1] = ch;

                        {

                              for (int k = 0; k < outstring.length; k++)

                                    System.out.print(outstring[k]);

                              System.out.println();

                        }

                  }

 

                  System.out.println();

                  System.out.println();

 

                  for (int i = 0; i < len; i++)

                  {

                        char ch = outstring[len - 1];

                        for (int j = len - 1; j > 0; j--)

                        {

                              outstring[j] = outstring[j - 1];

                        }

                        outstring[0] = ch;

                        {

                              for (int k = 0; k < outstring.length; k++)

                                    System.out.print(outstring[k]);

                              System.out.println();

                        }

                  }

                 

            }

            catch (Exception e)

            {

                  e.printStackTrace();

            }

    }

}

 

 

Output


 

D:\Program Files\Java\jdk1.6.0_23\bin>Javac StringRotate.java

 

D:\Program Files\Java\jdk1.6.0_23\bin>Java StringRotate

Enter a string to rotate:

Welcome to softwareandfinance.com

elcome to softwareandfinance.comW

lcome to softwareandfinance.comWe

come to softwareandfinance.comWel

ome to softwareandfinance.comWelc

me to softwareandfinance.comWelco

e to softwareandfinance.comWelcom

 to softwareandfinance.comWelcome

to softwareandfinance.comWelcome

o softwareandfinance.comWelcome t

 softwareandfinance.comWelcome to

softwareandfinance.comWelcome to

oftwareandfinance.comWelcome to s

ftwareandfinance.comWelcome to so

twareandfinance.comWelcome to sof

wareandfinance.comWelcome to soft

areandfinance.comWelcome to softw

reandfinance.comWelcome to softwa

eandfinance.comWelcome to softwar

andfinance.comWelcome to software

ndfinance.comWelcome to softwarea

dfinance.comWelcome to softwarean

finance.comWelcome to softwareand

inance.comWelcome to softwareandf

nance.comWelcome to softwareandfi

ance.comWelcome to softwareandfin

nce.comWelcome to softwareandfina

ce.comWelcome to softwareandfinan

e.comWelcome to softwareandfinanc

.comWelcome to softwareandfinance

comWelcome to softwareandfinance.

omWelcome to softwareandfinance.c

mWelcome to softwareandfinance.co

Welcome to softwareandfinance.com