Suppose somePhrase is any String and assume that it has alre

Suppose somePhrase is any String, and assume that it has already been created. Also, assume that a second String variable, anotherPhrase, has already been declared. Using the StringBuilder class, write code to place in anotherPhrase a palindrome version of somePhrase with a * character in the middle. For example, if somePhrase initially is \"abcd\", then anotherPhrase should ultimately hold \"abcd*dcba\"; and if somePhrase holds \"Oh dear!\", then ultimately anotherPhrase should hold \"Oh dear!*!raed hO\". When you\'ve built the final version of anotherPhrase, then print it to the console. Hint: first make two distinct StringBuilder versions of somePhrase. Then reverse the second one and use the append method to assemble and then print your final answer. The reverse method is tricky. If s and t are StringBuilder variables, and if s references \"xyz\", then the statement t = s.reverse(); puts the reverse of \"xyz\", \"yzx\", in both s and t. Enter your sequence of commands, ending with a print statement, in the box below.

Solution

Hi, Please find my code.

Please let me know in case of any issue.

import java.util.Scanner;

public class PalindromePhrase {

  

   public static void main(String[] args) {

      

       Scanner sc = new Scanner(System.in);

      

       // taking user input for some phrase

       System.out.println(\"Enter some phrase: \");

       String somePhrase = sc.nextLine();

      

       // building two StringBuilder from somePhrase

       StringBuilder somePhraseBuilder1 = new StringBuilder(somePhrase);

       StringBuilder somePhraseBuilder2 = new StringBuilder(somePhrase);

      

       // revering somePhraseBuilder2

       somePhraseBuilder2.reverse();

      

       // appending * in somePhraseBuilder1

       somePhraseBuilder1.append(\"*\");

      

       // now creating anotherPhrase  

       StringBuilder anotherPhrase = somePhraseBuilder1.append(somePhraseBuilder2);

      

       // printing on COnsole

       System.out.println(anotherPhrase);

   }

}

/*

Sample run:

Enter some phrase:

Oh My God!

Oh My God!*!doG yM hO

*/

Suppose somePhrase is any String, and assume that it has already been created. Also, assume that a second String variable, anotherPhrase, has already been decla
Suppose somePhrase is any String, and assume that it has already been created. Also, assume that a second String variable, anotherPhrase, has already been decla

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site