COSC 135B Questions on Characters and String methods
Tom Linton, Fall 2000, Central College
  1. For each code fragment below, what would be printed in the messageBox?
    1. String myName = "Tom Linton";

    2. messageBox( myName.charAt(1) );
       
       
       
       
       
    3. String myName = "Tom Linton";

    4. char aSpace = ' ';
      messageBox("Your first name has " + myName.indexOf(aSpace) + " letters.");
       
       
       
    5. public boolean isVowel(char aLetter){

    6.    boolean answer = false;
         switch(aLetter){
            case 'a': case 'e': case 'i': case 'o': case 'u': case 'y':
            case 'A': case 'E': case 'I': case 'O': case 'U': case 'Y':
               answer = true;
               break;
         }
         return answer;
      }
      int numVowels = 0;
      String myName = "Tommy Linton";
      for (int letter = 0; letter < myName.length(); letter++) {
         if ( isVowel( myName.charAt(letter) ) )
            numVowels++;
      }
      messageBox("The String " + myName + " has " + numVowels + " vowels.");
       
       
       
       
       
       
    7. String s1 = "Tom";

    8. String s2 = "tom";
      String s3 = s2.toUpperCase();
      messageBox("" + s1.equals(s3) + ", " + s1.equalsIgnoreCase(s3) +
            ", " + s2.length() == s3.length() + ", " + Character.isDigit( s1.charAt(1) ) );
       
       
       
       
       
  2. Write a method named charToUpper, which takes two inputs, a String, say s1 and an int, say n, and returns the String obtained by changing the character at location n in s1 to uppercase. For example, charToUpper("tom", 0) = "Tom" and charToUpper("OOPS",2) = "OOPS". HINT: You might just "add" 'A' - 'a' to some character.

  3.  

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

  4. How do you create a String that is the String version of an int variable? For example, if years = 125 is an int variable, how do I get a String version of years (namely, the digit 1 followed by the digit 2 followed by the digit 5)?