Reverse Each Word
Always indent your code
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
Post a Comment