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

java 1: reverseStringInWord: Please fill the function String reverseStringInWord

ID: 3809548 • Letter: J

Question

java

1: reverseStringInWord: Please fill the function String reverseStringInWord(String str) to return a string which reverse the input word by word. (Note: the words are separated by one more whitespace.)

Example 1: Input: (“I am good”); Return: good am I Example 2: Input: (“How are you”) Return you are How

2:  MagicString: Please fill the function boolean magicString(String str) to check if a string str is a magic string. A magic String is the one which can be constructed by repeating the substring of it. (Note: you can assume all the characters are in lowercase).

Example 1: Input: abcabcabc Return: True Example 2: Input: ababc Return False

Explanation / Answer

PROGRAM CODE:

package array;

public class StringFunctions {

   public static String reverseStringInWord(String str)

   {

       String words[] = str.split("\s+");

       String result = "";

       for(int i=words.length-1; i>=0; i--)

       {

           result += words[i] + " ";

       }

       return result;

   }

  

   public static boolean magicString(String str)

   {

       int counter = 0;

       for(int i=0;i<str.length();i++)

       {

char a=str.charAt(i);

char b=str.charAt(i+1);

int x=(int)a;

int y=(int)b;

if(y==x+1)

{

return true;;

}

}

   return false;

  

   }

   public static void main(String[] args) {

       System.out.println(reverseStringInWord("helloo how are you"));

       magicString("abcbabc");

   }

}