Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that prompts the user for a string and then prints out the same

ID: 3671756 • Letter: W

Question

Write a program that prompts the user for a string and then prints out the same string shifted by one character to the right. The last character should become the first one after that shift. You should use toCharArray() method from the String class to convert the String that you read from the user to individual characters. The array of characters should be then shifted by one character to the right. For example: If the user enters "Hello", the shifted string is "oHell". If the user enters "Key Terms", the shifted string is "sKey Term". Once the shift is done, the character array should be converted back to string and displayed to the user. The shift operation should be performed by a method with the following header Implement a second method that shifts a string by an arbitrary number of letters. The number should be provided as a parameter to the method: Use only arrays, do not use ArrayList!

Explanation / Answer

/**The java program test the method shift that takes a string
* value and number of shifts on string . The program
* prints the results of the string after calling shift
* method */
//ShiftString.java
public class ShiftString
{
   public static void main(String[] args)
   {
      
      
       //Set string str="Hello"
       String str="Hello";
      
       //Set number of shifts
       int numShifts=1;
      
       System.out.println("String : "+str);
       System.out.println("Number of shifts = "+numShifts);      
       System.out.println(shift(str,numShifts));
      
       //Set number of shifts
       numShifts=2;
      
       System.out.println("String : "+str);
       System.out.println("Number of shifts = "+numShifts);
       System.out.println(shift(str,numShifts));
      
       //Set number of shifts
       numShifts=3;      
       System.out.println("String : "+str);
       System.out.println("Number of shifts = "+numShifts);
       System.out.println(shift(str,numShifts));
      
      
      
      
   }

  
   /**The method shift that takes string str and integer numShifts
   * as input arguments and returns the string after shifting
   * numShift times*/
   private static String shift(String str, int numShifts)
   {
      
       //convert str to the character letters
       char letters[]=str.toCharArray();
       //declare a newstring
       String newstring=null;
      
      
       //for loop for number of shifts
       for (int index = 1; index <=numShifts; index++)
       {
          
           int last=letters.length-1;
           //get last character
           char lastChar=letters[last];
          
           for (int i = 0; i < letters.length-1; i++)
           {          
               letters[last]=letters[last-1];
               last--;
           }
          
           //Set last character to first
           letters[0]=lastChar;
          
           //convert letters to string
           newstring=new String(letters);
          
           //convert newstring to letters
           letters=newstring.toCharArray();
       }      
       //return newstring
       return newstring;
      
   }//end of shift method
  
}//end of class


------------------------------------------------------------------------------------------------------------------

Sample Output:

String : Hello
Number of shifts = 1
oHell
String : Hello
Number of shifts = 2
loHel
String : Hello
Number of shifts = 3
lloHe

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote