COSC 135B Questions on
Characters and String methods
Tom Linton,
Fall 2000, Central College
-
For each code fragment below, what would be printed in the messageBox?
-
String myName = "Tom Linton";
messageBox( myName.charAt(1) );
-
String myName = "Tom Linton";
char aSpace = ' ';
messageBox("Your first name has " + myName.indexOf(aSpace) + "
letters.");
-
public boolean isVowel(char aLetter){
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.");
-
String s1 = "Tom";
String s2 = "tom";
String s3 = s2.toUpperCase();
messageBox("" + s1.equals(s3) + ", " + s1.equalsIgnoreCase(s3)
+
", " + s2.length() == s3.length()
+ ", " + Character.isDigit( s1.charAt(1) ) );
-
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.
-
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)?