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

Create two STATIC string-manipulation methods in your *.java template just below

ID: 3815051 • Letter: C

Question

Create two STATIC string-manipulation methods in your

*.java template just below the public static void main() { } method/code.

Static method 1 takes in a String as a parameter and returns an array of characters. Given

what it does, it would make sense to call this method strToCharArray. In this method, you

will need to:

Create a character array of the appropriate size (recall the String.length() method)

Iterate through the string and copy each character from the string to the character

array (again, recall String.length() and String.charAt())

Return the array you created

Static method 2 will take in a character array (returns nothing) and simply prints out all

of the elements of the array in reverse order (you might call this method reversePrint).

Finally, write a main() method that tests all of your code. Your main method should

prompt the user to enter in a word (String). That String should be passed into method 1.

The returned result of method 1 should be passed into method 2 to fully test both

methods.

HINT: If you’ve done everything correctly, you should be able to use one statement in your

main method (once you’ve read in a String from the user into a variable called str) to test

your two methods: reversePrint(strToCharArray(str));

Explanation / Answer

StringMethodTest.java

import java.util.Scanner;


public class a {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter the string: ");
       String str = scan.nextLine();
       char ch [] = strToCharArray(str);
       reversePrint(ch);
   }
   public static char[] strToCharArray(String s){
       char ch[] = new char[s.length()];
       for(int i=0; i<s.length(); i++){
           ch[i]=s.charAt(i);
       }
       return ch;
   }
   public static void reversePrint(char ch[]){
       for(int i=ch.length-1; i>=0;i--) {
           System.out.print(ch[i]);
       }
       System.out.println();
   }

}

Output:

Enter the string:
Hello World
dlroW olleH