Created
March 11, 2013 20:55
-
-
Save knowbody/5137667 to your computer and use it in GitHub Desktop.
Reverse String algorithm
This file contains hidden or 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
| 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