ReverseString.java


Below is the syntax highlighted version of ReverseString.java from §3.1 Using Data Types.


/******************************************************************************
 *  Compilation:  javac ReverseString.java
 *  Execution:    java ReverseString s
 *
 *  Takes a string command-line argument and reverses its characters.
 *
 *  % java Reverse accttagggcatc
 *  ctacgggattcca
 *
 ******************************************************************************/

public class ReverseString {
    public static String reverse(String s) {
        int n = s.length();
        char[] a = s.toCharArray();
        for (int i = 0; i < n/2; i++) {
            char temp = a[i];
            a[i] = a[n-i-1];
            a[n-i-1] = temp;
        }
        return new String(a);
    }


    public static void main(String[] args) {
        StdOut.println(reverse(args[0]));
    }

}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:22:01 EDT 2022.