Reverse Each Word

 

The task is to implement a function so as to print the sentence such that each word in the sentence is reversed.

Sample Input 1:
Always indent your code
Sample Output 1:
syawlA tnedni ruoy edoc
public class Solution {

	public static String reverseEachWord(String str) {
		
        str=str+ " ";
       int l=str.length();
        String s="";
        String p="";
      for(int i=0;i<l;i++)
      {
          char ch=str.charAt(i);
          if(ch!=' ')
          {
              p=ch+p;
          }
          else
          {
              s=s+p+" ";
              p="";
          }
      }
       return s;

}
}

Comments

Popular posts from this blog

Minimum Length Word

Check Number Sequence

Star Pattern