Skip to content

Instantly share code, notes, and snippets.

@shailrshah
Created April 14, 2017 18:11
Show Gist options
  • Save shailrshah/089b401553fa7c6ed8649c039e7ce1cd to your computer and use it in GitHub Desktop.
Save shailrshah/089b401553fa7c6ed8649c039e7ce1cd to your computer and use it in GitHub Desktop.
Reverse all Words of a given String
public class WordReverse {
static String reverse(String str){
char[] word = str.toCharArray();
for(int i = 0; i < word.length/2; i++){
char temp = word[i];
word[i] = word[word.length-1-i];
word[word.length-1-i] = temp;
}
return new String(word);
}
static String reverseAllWords(String str){
String[] words = str.split(" ");
String finalString = "";
for(int i = 0; i < words.length; i++){
finalString+=reverse(words[i]);
if(i < words.length-1)
finalString+=" ";
}
return finalString;
}
public static void main(String[] args){
System.out.println(reverseAllWords("One Two Three Four Five"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment