Skip to content

Instantly share code, notes, and snippets.

@knowbody
Created March 11, 2013 20:55
Show Gist options
  • Select an option

  • Save knowbody/5137667 to your computer and use it in GitHub Desktop.

Select an option

Save knowbody/5137667 to your computer and use it in GitHub Desktop.
Reverse String algorithm
import javax.swing.JOptionPane;
public class ReverseString {
public static String reverse(String s) {
int length = s.length(), last = length - 1;
char[] chars = s.toCharArray();
for (int i = 0; i < length / 2; i++) {
char c = chars[i];
chars[i] = chars[last - i];
chars[last - i] = c;
}
return new String(chars);
}
public static void main(String[] args) {
String reverseInput = JOptionPane.showInputDialog("Type to reverse");
String w = reverse(reverseInput);
System.out.println(w);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment