University of Toronto - Fall 2000
Department of Computer Science

Week 7 - String Reversal Example

TestReverse.java - Version 1

/**
 * TestReverse: This program takes input from the user, and
 *	prints out the reverse of each line of input.  The program
 *	stops when the user enters "quit"
 */
import java.io.*;
public class TestReverse {
	public static void main (String[] args) throws IOException {

		BufferedReader getit;
		getit = new BufferedReader
			  (new InputStreamReader (System.in));

		// Prompt user to enter text.
		System.out.println ("Enter lines of text.");
		System.out.println ("Type quit to stop.");
		System.out.flush ();

		// Read first line before loop.
		String text = getit.readLine();
		
		// Loop until the user enters quit
		while (true) {

			// Leave loop if word was quit
			if (text.equals("quit"))
				break;

			// Print out reverse of word
			System.out.println (reverse(text));

			// Read next line of text
			text = getit.readLine();
		}
		System.out.println ("End of program.");
	}
	
	/*
	 * reverse: return value is same as parameter "original"
	 *		  reversed.
	 */
	public static String reverse (String original) {

		String reversed = ""; 	// where we build the reversed string

		// where are we in original?
		int pos = original.length() - 1;

		// for each character ...
		while (pos >= 0) {
			// "write it down"
			reversed = reversed + original.charAt(pos);
			pos -= 1;  // more of "for each character"
		}
		return reversed;
	}

}

TestReverse2.java - Version 2

In this version, we have moved the readLine() into the loop, and have eliminated the call to readline() at the end of the loop.

In this version of the reverse method, we take each character from the original string, starting from character 0 and going to the last character in the original string. We insert each character to the beginning of the new reversed string.

/**
 * TestReverse2: This program takes input from the user, and
 *	prints out the reverse of each line of input.  The program
 *	stops when the user enters "quit"
 */
import java.io.*;
public class TestReverse2 {
	public static void main (String[] args) throws IOException {

		BufferedReader getit;
		getit = new BufferedReader
			  (new InputStreamReader (System.in));

		// Prompt user to enter text.
		System.out.println ("Enter lines of text.");
		System.out.println ("Type quit to stop.");
		System.out.flush ();

		// Loop until the user enters quit
		while (true) {
			// Read a line of text
			String text = getit.readLine();

			// Leave loop if word was quit
			if (text.equals("quit"))
				break;

			// Print out reverse of word
			System.out.println (reverse(text));
		}
		System.out.println ("End of program.");
	}
	
	/*
	 * reverse: return value is same as parameter "original"
	 *		  reversed.
	 */
	public static String reverse (String original) {

		String reversed = ""; 	// where we build the reversed string

		// where are we in original?
		int pos = 0;

		// for each character ...
		while (pos < original.length()) {
			reversed = original.charAt(pos) + reversed;
			pos += 1;
		}
		return reversed;
	}

}

Output - same for both versions

Enter lines of text.
Type quit to stop.
Hello
olleH
Montreal
laertnoM
See you later!
!retal uoy eeS
 quit
tiuq
quit
End of program.