Skip to content

Instantly share code, notes, and snippets.

@xnorcode
Last active August 29, 2018 16:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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