Last active
August 29, 2018 16:14
-
-
Save xnorcode/9b2945902d3a01ee823e48f8c132dd4d to your computer and use it in GitHub Desktop.
Revesring a string with arrays
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
... | |
// convert String to array of chars | |
char[] s = str.toCharArray(); | |
// get length of string | |
int n = s.length; | |
// get middle of string | |
int middle = n/2; | |
// swap characters in the array | |
for(int i = 0; i< middle; i++){ | |
char tmp = s[i]; | |
s[i] = s[n-1-i]; | |
s[n-1-i] = tmp; | |
} | |
// print reversed string | |
System.out.println(new String(s)); | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment