Friday 3 December 2021

Q11_Program to determine whether one string is a rotation of another

Rotate

Program to determine whether one string is a rotation of another

In this program, we need to check whether a string is a rotation of another string or not.

Consider the above example, suppose we need to check whether string 2 is a rotation of string 1. To find this, we concatenate string 1 with string 1. Then, try to find the string 2 in concatenated string. If string 2 is present in concatenated string then, string 2 is rotation of string 1. String 2 deabc is found on the index 3 in concatenated string. So, deabc is rotation of abcde.

string 1

abcde

string 2

deabc

string 1 String2

abcdeabcde

Program

class one_string_rotation {

  public static void main(String[] args)   {

    //declare string

   String str1 = "abcde", str2 = "deabc"; ;

  if (str1.length() != str2.length())

   {

     System.out.println( "second string not rotate" );

   }

 else

  {

  str1=str1.concat(str1);

  if (str1.indexOf(str2)!=-1)

  {

    System.out.println( "second string rotate frist string" );

  }

else

{

  System.out.println( "second string rotate frist string" );

  }

    }

   }

output

Second string is a rotation of first string

Theory

in this program we have roate one string is roate to each other or not. we have to conact one string is itself then found the second string in concated frist string its that means a string is a rotation of another string. if firsr of all check lenght of both strings are equal then to chack wether a string is a rotation of another string . lenghts are not equals the stringits second string is not rotate first.

No comments:

Post a Comment

How do you select data from a table in SQL?

Create Table How do you select data a table in SQL? The SELECT statement is used to se...