Created
September 16, 2018 20:20
-
-
Save deyindra/42c9b5f819e54dc7df3d96a0d85f6aea to your computer and use it in GitHub Desktop.
Reverse Word
This file contains 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 static String reverseWord(String str){ | |
char[] array = str.toCharArray(); | |
int start=0; | |
int end=array.length-1; | |
reverse(array,0,array.length-1); | |
while (start<=end && array[start]==' ') | |
start++; | |
while (end>=start && array[end] == ' ') | |
end--; | |
for(int j=start;j<=end;j++){ | |
if(array[j]==' '){ | |
reverse(array,start,j-1); | |
j++; | |
while (array[j]==' ') | |
j++; | |
start=j; | |
} | |
} | |
reverse(array,start,end); | |
return String.valueOf(array); | |
} | |
public static void reverse(char[] array, int start, int end){ | |
while (start<end){ | |
char temp = array[start]; | |
array[start]=array[end]; | |
array[end]=temp; | |
start++; | |
end--; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment