Menu Close

Match string with rotated string in java …ftc

Write a java Java program to check whether one string is rotation of another string.

Input:
string s1: summonlord
string s2: lordsummon

Output:
s2 is a rotated version of s1
import java.io.*;
import java.util.*;
public class temp{
  static boolean matchstr(String str1, String str2){
    if(str1.length()!=str2.length()){
      return false;
    }
    String str3 = str1+str1;
    if(str3.contains(str2)){
      return true;
    }
    else{
      return false;
    }
  }

  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    System.out.print("String s1:  ");
    String str1 = scan.nextLine();
    System.out.print("String s2:  ");
    String str2 = scan.nextLine();

    if(matchstr(str1,str2)){
      System.out.println("s2 is a rotated version of s1");
    }
    else{
      System.out.println("s2 is not a rotated version of s1");
    }
	}

}

INPUT_1:
String s1:  string1234
String s2:  1234string

OUTPUT:
s2 is a rotated version of s1


INPUT_2:
String s1:  kakeguruicircus
String s2:  circuskakegurui

OUTPUT:
s2 is a rotated version of s1


INPUT_3:
String s1:  color101
String s2:  101color

OUTPUT:
s2 is a rotated version of s1


INPUT_4:
String s1:  demonslayer
String s2:  slayerdemon

OUTPUT:
s2 is a rotated version of s1


INPUT_5:
String s1:  demongod
String s2:  demon

OUTPUT:
s2 is not a rotated version of s1


INPUT_6:
String s1:  pokemon
String s2:  pokemon

OUTPUT:
s2 is a rotated version of s1


INPUT_7:
String s1:  fcukthecode
String s2:  codethefcuk

OUTPUT:
s2 is not a rotated version of s1


INPUT_8:
String s1:  fcukthecode
String s2:  thecodefcuk

OUTPUT:
s2 is a rotated version of s1


ILLUSTRATION

Executed using javac on linux terminal