Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Revesring a string with arrays
...
// 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