Basic HTML version of Foils prepared 17 Nov 97

Foil 23 ReverseString Class

From Java Tutorial, July 1, 1996 CEWES Tutorial, CPS606, JSU Class CSC499 -- July 22-25 1997, Fall 97. by Nancy J. McCracken,Geoffrey C. Fox, Tom Scavo

A pizza order example.  The bill display uses stringbuffer to line up and display dollar amounts.
This class returns an object of class String that reverses order of characters in its argument:
class ReverseString {
  • public static String reverse( String s ) {
  • int i, len = s.length();
  • StringBuffer dest = new StringBuffer(len);
  • for( i = (len-1); i >= 0 ; i-- ) {
  • dest.append( s.charAt(i) );
  • }
  • return dest.toString();
  • }
}
Or more simply:
StringBuffer sb = new StringBuffer( s );
return sb.reverse().toString();



© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Wed Apr 1 1998