Skip to content

Instantly share code, notes, and snippets.

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
Save xnorcode/9b2945902d3a01ee823e48f8c132dd4d to your computer and use it in GitHub Desktop.
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