// UplowLowercase.java Copyright (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2006-02-23 File created. // 2006-02-23 Last modification. // UplowLowercase.java Copyright (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2006-02-23 File created. // 2006-02-23 Last modification. // A solution to Exercise 16-2. class UplowLowercase { static String convert_string_lowercase( String given_string ) { StringBuilder modified_string = new StringBuilder( given_string ) ; int character_index = 0 ; while ( character_index < modified_string.length() ) { if ( modified_string.charAt( character_index ) >= 'A' && modified_string.charAt( character_index ) <= 'Z' ) { modified_string.setCharAt( character_index, (char) ( modified_string.charAt( character_index ) | 0x20 ) ); } character_index ++ ; } return modified_string.toString() ; } static String convert_string_uppercase( String given_string ) { StringBuilder modified_string = new StringBuilder( given_string ) ; int character_index = 0 ; while ( character_index < modified_string.length() ) { if ( modified_string.charAt( character_index ) >= 'a' && modified_string.charAt( character_index ) <= 'z' ) { modified_string.setCharAt( character_index, (char) ( modified_string.charAt( character_index ) & 0xDF ) ); } character_index ++ ; } return modified_string.toString() ; } public static void main( String[] not_in_use ) { String test_string = "James Gosling is the inventor of Java." ; System.out.print( "\n Original string: " + test_string ) ; String uppercase_string = convert_string_uppercase( test_string ) ; System.out.print( "\n After conversion: " + uppercase_string ) ; System.out.print( "\n Better conversion: " + test_string.toUpperCase() ) ; String lowercase_string = convert_string_lowercase( test_string ) ; System.out.print( "\n After lowercasing: " + lowercase_string ) ; System.out.print( "\n Better lowercasing:" + test_string.toLowerCase() ) ; } }