Created
April 14, 2017 18:11
-
-
Save shailrshah/089b401553fa7c6ed8649c039e7ce1cd to your computer and use it in GitHub Desktop.
Reverse all Words of a given String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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