ANSWERS TO STRING PROCESSING PROBLEMS // Problem 1. public static void main(String[] args) { String s, upperS; int i, vowelCount = 0; char x; s = JOptionPane.showMessageDialog("Enter a string."); upperS = s.toUpperCase(); for(i = 0; i < s.length(); i++) { x = upperS.charAt(i); if(x=='A' || x=='E' || x=='I' || x=='O' || x=='U') vowelCount++; } System.out.println("The number of vowels in " + s + " is " + vowelCount + "."); } // Problem 2. public static void main(String[] args) { String s; int i, upperCaseCount = 0; char x; s = JOptionPane.showMessageDialog("Enter a string."); for(i = 0; i < s.length(); i++) { x = s.charAt(i); if('A' <= x && x <= 'Z') upperCaseCount++; } System.out.println("The number of upper case letters " + s + " is " + upperCaseCount + "."); } // Problem 3. public static void main(String[] args) { String s; int i; char x; boolean flag = false; s = JOptionPane.showMessageDialog("Enter a string."); for(i = 0; i < s.length(); i++) { x = s.charAt(i); if('0' <= x && x <= '9') flag = true; } System.out.println(flag); } // Problem 4. public static void main(String[] args) { String s; int i; char x; boolean flag = true; s = JOptionPane.showMessageDialog("Enter a string."); for(i = 0; i < s.length(); i++) { x = s.charAt(i); if(x != '*') flag = false; } System.out.println(flag); } // Problem 5. public static void main(String[] args) { String s; int i; char x; boolean flag = false; s = JOptionPane.showMessageDialog("Enter a string."); for(i = 0; i < s.length(); i++) { x = s.charAt(i); if(x == 'x') flag = true; } System.out.println(flag); } // Problem 6. public static void main(String[] args) { String s; char x; boolean flag = false; s = JOptionPane.showMessageDialog("Enter a string."); if (s.length() == 0) System.out.println( "Length of string is 0."); else if(s.charAt(0) == s.charAt(s.length() - 1)) System.out.println( "String begins and ends with same letter."); else System.out.println( "String does not begin and end with same letter."); }