Posted on by Kalkicode
Code String

# Check whether one string is rotation of another

Here given code implementation process.

``````// Java program for
// Check whether one string is rotation of another

public class Rotation {

public void isRotationView(String str1, String str2)
{
int n = str1.length();

boolean result = false;

if(n == str2.length())
{

String s = "";
String e = "";

for (int i = 0; i < n-1 && result == false ; ++i)
{
// First part 0 to i
s = str1.substring(0,i+1);
// Second Part i+1 to n
e = str1.substring(i+1,n);

if((e+s).equals(str2))
{
// When (e + s) is equal to str2
result = true;
}

}
}
// Display given strings
System.out.println("\n Given Text str1 : "+str1);
System.out.println(" Given Text str2 : "+str2);

if(result==false)
{
System.out.println(" No ");
}
else
{
System.out.println(" Yes ");
}

}
public static void main(String[] args) {

String str1 = "XYZPINCODE";
String str2 = "PINCODEXYZ";
// Test A
/*
XYZPINCODE
----------

XYZ    PINCODE
---    -------
⤷    ⤶

PINCODE  XYZ
-------  ---

PINCODEXYZ == PINCODEXYZ
-------------------------
Result is Yes
*/

str1 = "XYZPINCODE";
str2 = "PINXYZCODE";
// Test B

}
}``````

#### Output

`````` Given Text str1 : XYZPINCODE
Given Text str2 : PINCODEXYZ
Yes

Given Text str1 : XYZPINCODE
Given Text str2 : PINXYZCODE
No``````
``````// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ program for
// Check whether one string is rotation of another
class Rotation
{
public: void isRotationView(string str1, string str2)
{
int n = str1.length();
bool result = false;
if (n == str2.length())
{
string s = "";
string e = "";
for (int i = 0; i < n - 1 && result == false; ++i)
{
// First part 0 to i
s = str1.substr(0, i + 1);
// Second Part i+1 to n
e = str1.substr(i + 1, n);
if ((e  +  s).compare(str2) == 0)
{
// When (e + s) is equal to str2
result = true;
}
}
}
// Display given strings
cout << "\n Given Text str1 : " << str1 << endl;
cout << " Given Text str2 : " << str2 << endl;
if (result == false)
{
cout << " No " << endl;
}
else
{
cout << " Yes " << endl;
}
}
};
int main()
{
string str1 = "XYZPINCODE";
string str2 = "PINCODEXYZ";
// Test A
/*
XYZPINCODE
----------

XYZ    PINCODE
---    -------
⤷    ⤶

PINCODE  XYZ
-------  ---

PINCODEXYZ == PINCODEXYZ
-------------------------
Result is Yes
*/
str1 = "XYZPINCODE";
str2 = "PINXYZCODE";
// Test B
return 0;
}``````

#### Output

`````` Given Text str1 : XYZPINCODE
Given Text str2 : PINCODEXYZ
Yes

Given Text str1 : XYZPINCODE
Given Text str2 : PINXYZCODE
No``````
``````// Include namespace system
using System;
// Csharp program for
// Check whether one string is rotation of another
public class Rotation
{
public void isRotationView(String str1, String str2)
{
int n = str1.Length;
Boolean result = false;
if (n == str2.Length)
{
String s = "";
String e = "";
for (int i = 0; i < n - 1 && result == false; ++i)
{
// First part 0 to i
s = str1.Substring(0,i+1);

// Second Part i+1 to n
e = str1.Substring(i + 1, n - (i+1) );

if ((e + s).Equals(str2))
{
// When (e + s) is equal to str2
result = true;
}
}
}
// Display given strings
Console.WriteLine("\n Given Text str1 : " + str1);
Console.WriteLine(" Given Text str2 : " + str2);
if (result == false)
{
Console.WriteLine(" No ");
}
else
{
Console.WriteLine(" Yes ");
}
}
public static void Main(String[] args)
{
String str1 = "XYZPINCODE";
String str2 = "PINCODEXYZ";
// Test A
/*
XYZPINCODE
----------

XYZ    PINCODE
---    -------
⤷    ⤶

PINCODE  XYZ
-------  ---

PINCODEXYZ == PINCODEXYZ
-------------------------
Result is Yes
*/
str1 = "XYZPINCODE";
str2 = "PINXYZCODE";
// Test B
}
}``````

#### Output

`````` Given Text str1 : XYZPINCODE
Given Text str2 : PINCODEXYZ
Yes

Given Text str1 : XYZPINCODE
Given Text str2 : PINXYZCODE
No``````
``````package main
import "fmt"
// Go program for
// Check whether one string is rotation of another

func isRotationView(str1, str2 string) {
var n int = len(str1)
var result bool = false
if n == len(str2) {
var s string = ""
var e string = ""
var runes = []rune(str1)
for i := 0 ; i < n - 1 && result == false ; i++ {
// First part 0 to i
s = string(runes[0:i+1])
// Second Part i+1 to n
e = string(runes[i + 1:n])
if (e + s) == str2 {
// When (e + s) is equal to str2
result = true
}
}
}
// Display given strings
fmt.Println("\n Given Text str1 : ", str1)
fmt.Println(" Given Text str2 : ", str2)
if result == false {
fmt.Println(" No ")
} else {
fmt.Println(" Yes ")
}
}
func main() {

var str1 string = "XYZPINCODE"
var str2 string = "PINCODEXYZ"
// Test A
/*
XYZPINCODE
----------
XYZ    PINCODE
---    -------
⤷    ⤶
PINCODE  XYZ
-------  ---

PINCODEXYZ == PINCODEXYZ
-------------------------
Result is Yes
*/
isRotationView(str1, str2)
str1 = "XYZPINCODE"
str2 = "PINXYZCODE"
// Test B
isRotationView(str1, str2)
}``````

#### Output

`````` Given Text str1 : XYZPINCODE
Given Text str2 : PINCODEXYZ
Yes

Given Text str1 : XYZPINCODE
Given Text str2 : PINXYZCODE
No``````
``````<?php
// Php program for
// Check whether one string is rotation of another
class Rotation
{
public	function isRotationView(\$str1, \$str2)
{
\$n = strlen(\$str1);
\$result = false;
if (\$n == strlen(\$str2))
{
\$s = "";
\$e = "";
for (\$i = 0; \$i < \$n - 1 && \$result == false; ++\$i)
{
// First part 0 to i
\$s = substr(\$str1, 0, \$i + 1) ;
// Second Part i+1 to n
\$e = substr(\$str1, \$i + 1, \$n);
if ((strcmp((\$e.\$s), \$str2) == 0))
{
// When (e + s) is equal to str2
\$result = true;
}
}
}
// Display given strings
echo("\n Given Text str1 : ".\$str1."\n");
echo(" Given Text str2 : ".\$str2."\n");
if (\$result == false)
{
echo(" No \n");
}
else
{
echo(" Yes \n");
}
}
}

function main()
{
\$str1 = "XYZPINCODE";
\$str2 = "PINCODEXYZ";
// Test A
/*
XYZPINCODE
----------
XYZ    PINCODE
---    -------
⤷    ⤶
PINCODE  XYZ
-------  ---

PINCODEXYZ == PINCODEXYZ
-------------------------
Result is Yes
*/
\$str1 = "XYZPINCODE";
\$str2 = "PINXYZCODE";
// Test B
}
main();``````

#### Output

`````` Given Text str1 : XYZPINCODE
Given Text str2 : PINCODEXYZ
Yes

Given Text str1 : XYZPINCODE
Given Text str2 : PINXYZCODE
No``````
``````// Node JS program for
// Check whether one string is rotation of another
class Rotation
{
isRotationView(str1, str2)
{
var n = str1.length;
var result = false;
if (n == str2.length)
{
var s = "";
var e = "";
for (var i = 0; i < n - 1 && result == false; ++i)
{
// First part 0 to i
s = str1.substring(0, i + 1);
// Second Part i+1 to n
e = str1.substring(i + 1, n);
if (((e + s).localeCompare(str2) == 0))
{
// When (e + s) is equal to str2
result = true;
}
}
}
// Display given strings
console.log("\n Given Text str1 : " + str1);
console.log(" Given Text str2 : " + str2);
if (result == false)
{
console.log(" No ");
}
else
{
console.log(" Yes ");
}
}
}

function main()
{
var str1 = "XYZPINCODE";
var str2 = "PINCODEXYZ";
// Test A
/*
XYZPINCODE
----------
XYZ    PINCODE
---    -------
⤷    ⤶
PINCODE  XYZ
-------  ---

PINCODEXYZ == PINCODEXYZ
-------------------------
Result is Yes
*/
str1 = "XYZPINCODE";
str2 = "PINXYZCODE";
// Test B
}
main();``````

#### Output

`````` Given Text str1 : XYZPINCODE
Given Text str2 : PINCODEXYZ
Yes

Given Text str1 : XYZPINCODE
Given Text str2 : PINXYZCODE
No``````
``````#  Python 3 program for
#  Check whether one string is rotation of another
class Rotation :
def isRotationView(self, str1, str2) :
n = len(str1)
result = False
if (n == len(str2)) :
s = ""
e = ""
i = 0
while (i < n - 1 and result == False) :
#  First part 0 to i
s = str1[0: i + 1]
#  Second Part i+1 to n
e = str1[i + 1: n]
if ((e + s) == str2) :
#  When (e + s) is equal to str2
result = True

i += 1

#  Display given strings
print("\n Given Text str1 : ", str1)
print(" Given Text str2 : ", str2)
if (result == False) :
print(" No ")
else :
print(" Yes ")

def main() :
str1 = "XYZPINCODE"
str2 = "PINCODEXYZ"
#  Test A
#    XYZPINCODE
#    ----------
#    XYZ    PINCODE
#    ---    -------
#      ⤷    ⤶
#    PINCODE  XYZ
#    -------  ---
#    PINCODEXYZ == PINCODEXYZ
#    -------------------------
#    Result is Yes
str1 = "XYZPINCODE"
str2 = "PINXYZCODE"
#  Test B

if __name__ == "__main__": main()``````

#### Output

`````` Given Text str1 :  XYZPINCODE
Given Text str2 :  PINCODEXYZ
Yes

Given Text str1 :  XYZPINCODE
Given Text str2 :  PINXYZCODE
No``````
``````#  Ruby program for
#  Check whether one string is rotation of another
class Rotation
def isRotationView(str1, str2)
n = str1.length
result = false
if (n == str2.length)
s = ""
e = ""
i = 0
while (i < n - 1 && result == false)
#  First part 0 to i
s = str1[0...i + 1]
#  Second Part i+1 to n
e = str1[i + 1...n]
if ((e + s) === str2)
#  When (e + s) is equal to str2
result = true
end

i += 1
end

end

#  Display given strings
print("\n Given Text str1 : ", str1, "\n")
print(" Given Text str2 : ", str2, "\n")
if (result == false)
print(" No ", "\n")
else

print(" Yes ", "\n")
end

end

end

def main()
str1 = "XYZPINCODE"
str2 = "PINCODEXYZ"
#  Test A
#    XYZPINCODE
#    ----------
#    XYZ    PINCODE
#    ---    -------
#      ⤷    ⤶
#    PINCODE  XYZ
#    -------  ---
#    PINCODEXYZ == PINCODEXYZ
#    -------------------------
#    Result is Yes
str1 = "XYZPINCODE"
str2 = "PINXYZCODE"
#  Test B
end

main()``````

#### Output

`````` Given Text str1 : XYZPINCODE
Given Text str2 : PINCODEXYZ
Yes

Given Text str1 : XYZPINCODE
Given Text str2 : PINXYZCODE
No
``````
``````// Scala program for
// Check whether one string is rotation of another
class Rotation()
{
def isRotationView(str1: String, str2: String): Unit = {
var n: Int = str1.length();
var result: Boolean = false;
if (n == str2.length())
{
var s: String = "";
var e: String = "";
var i: Int = 0;
while (i < n - 1 && result == false)
{
// First part 0 to i
s = str1.substring(0, i + 1);
// Second Part i+1 to n
e = str1.substring(i + 1, n);
if ((e + s).equals(str2))
{
// When (e + s) is equal to str2
result = true;
}
i += 1;
}
}
// Display given strings
println("\n Given Text str1 : " + str1);
println(" Given Text str2 : " + str2);
if (result == false)
{
println(" No ");
}
else
{
println(" Yes ");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Rotation = new Rotation();
var str1: String = "XYZPINCODE";
var str2: String = "PINCODEXYZ";
// Test A
/*
XYZPINCODE
----------
XYZ    PINCODE
---    -------
⤷    ⤶
PINCODE  XYZ
-------  ---

PINCODEXYZ == PINCODEXYZ
-------------------------
Result is Yes
*/
str1 = "XYZPINCODE";
str2 = "PINXYZCODE";
// Test B
}
}``````

#### Output

`````` Given Text str1 : XYZPINCODE
Given Text str2 : PINCODEXYZ
Yes

Given Text str1 : XYZPINCODE
Given Text str2 : PINXYZCODE
No``````
``````// Kotlin program for
// Check whether one string is rotation of another
class Rotation
{
fun isRotationView(str1: String, str2: String): Unit
{
val n: Int = str1.length;
var result: Boolean = false;
if (n == str2.length)
{
var s: String ;
var e: String ;
var i: Int = 0;
while (i < n - 1 && result == false)
{
// First part 0 to i
s = str1.substring(0, i + 1);
// Second Part i+1 to n
e = str1.substring(i + 1, n);
if ((e + s).equals(str2))
{
// When (e + s) is equal to str2
result = true;
}
i += 1;
}
}
// Display given strings
println("\n Given Text str1 : " + str1);
println(" Given Text str2 : " + str2);
if (result == false)
{
println(" No ");
}
else
{
println(" Yes ");
}
}
}
fun main(args: Array < String > ): Unit
{
var str1: String = "XYZPINCODE";
var str2: String = "PINCODEXYZ";
// Test A
/*
XYZPINCODE
----------
XYZ    PINCODE
---    -------
⤷    ⤶
PINCODE  XYZ
-------  ---

PINCODEXYZ == PINCODEXYZ
-------------------------
Result is Yes
*/
str1 = "XYZPINCODE";
str2 = "PINXYZCODE";
// Test B
}``````

#### Output

`````` Given Text str1 : XYZPINCODE
Given Text str2 : PINCODEXYZ
Yes

Given Text str1 : XYZPINCODE
Given Text str2 : PINXYZCODE
No``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post