public static String rotateString( String s. int n) Return the result of rotatin
ID: 3529970 • Letter: P
Question
public static String rotateString( String s. int n) Return the result of rotating the string n places to the right, circularly: rotateString( "Pineapple". 0) returns "Pineapple" rotateString( "Pineapple". 1) returns "ePineappl" rotateString( "Pineapple". 2) returns "lePineapp" rotateString( "Pineapple". 8) returns "ineappleP" rotateString( "Pineapple". 9) returns "Pineapple" rotateString( "Pineapple". 900000000) returns "Pineapple" rotateString( "Pineapple". 900000001) returns "ePineappl" rotateString( "Pineapple". -1) returns "ineappleP" rotateString( "Pineapple". -8) returns "ePineappl" rotateString( "Pineapple". -900000001) returns "ineappleP" No I/O. public static bool digit Sequence( String s) Returns whether s is a valid digitSequence that looks like an int. We return true only if s is a sequence of >0 chars, and every char is a digit in ['O','9'] OR s is a hyphen followed by a sequence of >0 chars, e.g., "0" true (simplest case) false (empty String doesn't qualify here) "0123456789" true long strings are fine: "-9999999999999999999999999999999999999999999999999999999" true "+1234" false (the only sign allowed is a single hyphen) "-0" true (another name for 0) " 888888888888888888888888888888888888" false (white space not ok) 000000000000000000000000000000000000000012" true (leading 0' s are fine)Explanation / Answer
This is the exact answer for your question no 2 : so check it out and do rate...I m posting the answer to que3 in another post..so that also..
Code starts from here
import java.util.Scanner;
public class RotateString {
public static String rotateString(String toRotate,long noOfPlaces)
{
String newString1="";
String newString2="";
int actualno=0;
String no=noOfPlaces+"";
if(Math.abs(noOfPlaces) > toRotate.length())
{
actualno=(int)Math.abs(noOfPlaces) % toRotate.length();
}
else
{
actualno=(int)Math.abs(noOfPlaces);
}
if(noOfPlaces >= 0)
{
newString1=toRotate.substring(toRotate.length()-actualno);
newString2=toRotate.substring(0,toRotate.length()-actualno);
}
else
{
newString2=toRotate.substring(0,actualno);
newString1=toRotate.substring(actualno,toRotate.length());
}
return newString1+newString2;
}
public static void main(String []args)
{
int choice =0;
do
{
Scanner sc=new Scanner(System.in);
System.out.println("Please Enter the String to rotate");
String rotate=sc.next();
System.out.println("Enter the no of places");
long no=sc.nextLong();
System.out.println("The rotated String is : "+rotateString(rotate,no));
System.out.println("Enter another String ?");
System.out.println("Press 1 to Yes and any other key to No");
choice=sc.nextInt();
}while(choice==1);
}
}
sample output :
Please Enter the String to rotate
Pineapple
Enter the no of places
2
The rotated String is : lePineapp
Enter another String ?
Press 1 to Yes and any other key to No
1
Please Enter the String to rotate
Pineapple
Enter the no of places
900000001
The rotated String is : ePineappl
Enter another String ?
Press 1 to Yes and any other key to No
1
Please Enter the String to rotate
Pineapple
Enter the no of places
-2
The rotated String is : neapplePi
Enter another String ?
Press 1 to Yes and any other key to No
1
Please Enter the String to rotate
Pineapple
Enter the no of places
-900000002
The rotated String is : neapplePi
Enter another String ?
Press 1 to Yes and any other key to No
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.