Rotating String Anticlockwise by N places - Java

another program : Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s.


import java.util.Arrays;

public class testCode1 {
public static void main(String[] args) {

String value="swati";
String goal = "tiswa";
testCode1 ts = new testCode1();
boolean flag=false;

for(int i=0;i<value.length()-1;i++){

if(ts.rotateString(value,i).equals(goal)){
flag=true;
break;
}else {
flag=false;
}
}

if(flag){
System.out.println("rotaion is found");
}else{
System.out.println("rotaiton not found");
}

}

public String rotateString(String value,int rotate){

//value = "swati"; // azonam
int val = value.length()-2;
//rotate=2;

char arr[]=new char[value.length()];
for(int i=0;i<value.length()-rotate;i++){
arr[i]=value.toCharArray()[i+rotate];
}

for(int i=0;i<rotate;i++){
arr[(value.length())-(rotate-i)]=value.toCharArray()[i];
}
String s="";
for(int i=0;i<arr.length;i++){
s=s+arr[i];
}
return s;

}



}

 



logic

public class testCode1 {

    public static void main(String[] args) {

String value = "swati"; // azonam
int val = value.length()-2;
int rotate=2;

char arr[]=new char[value.length()];
for(int i=0;i<value.length()-rotate;i++){
arr[i]=value.toCharArray()[i+rotate];
}

for(int i=0;i<rotate;i++){
arr[(value.length())-(rotate-i)]=value.toCharArray()[i];
}
String s="";
for(int i=0;i<arr.length;i++){
s=s+arr[i];
}

System.out.println(s);
}
}

Comments

Popular posts from this blog

Rest Assured

Sort (Bubble) - Java

Rotate the String from position - Java