CS110 Lab 15
Introduction to Strings and String Manipulation
Part 1: Notes for comparing strings.
NOTE: Strings should be compared using the equals() method from the String class.
Perform the following experiment.
String message1 = "CUPCAKE";
String message2 = "CUP";
message1 = message1.substring(0, 3);
if (message1 == message2)
System.out.println(message1 + " equals " + message2);
else
System.out.println(message1 + " is not equal to " + message2);
if (message1.equals(message2))
System.out.println(message1 + " equals " + message2);
else
System.out.println(message1 + " is not equal to " + message2);
Part 2: Coding Practice with Strings
Practice problem 1
Write an program that allows the user to input a sentence.
Create a new sentence with all vowels converted to way.
TIP: Create a method isVowel() that receives a character and returns true if it is a vowel and false otherwise.
For example:
Input: April's french fries are getting cold.
Output:wayprwayl's frwaynch frwayways wayrway gwayttwayng cwayld.
Practice problem 2
Write a short program that reads a sentence.
Count the number of words in the sentence and display each word on an individual line.
TIP: For this problem, assume that words are separated by white space (spaces or tabs).
This means that you can use a Scanner to process a string.
Example: run spot run
Output: The sentence contains 3 words.
run
spot
run
Practice problem 3
Write a method countYZ() that given a string, counts the number of words ending in 'y' or 'z' -- so the 'y' in "heavy" and the 'z' in "fez"
count, but not the 'y' in "yellow" (not case sensitive). For this problem, assume that words end with non-alphabetic characters.
This means that a y or z is at the end of a word if there is not an alphabetic letter immediately following it.
(TIP: Character.isLetter(char) tests if a char is an alphabetic letter.)
Perform the following tests:
- countYZ("fez day") ----->2
- countYZ("!!day:yak") ----->1
- countYZ("yak zak") -----> 0
Practice problem 4
Assume that a lowercase 'g' in a string is "happy" if there is another 'g' immediately to its left or right.
Write a method gHappy() that given a string,returns true if all the g's in the given string are happy.
Perform the following tests:
- gHappy("xxggxx") -----> true
- gHappy("xxgxx") -----> false
- gHappy("xxggyygxx") -----> false
Practice problem 5
Write a method sumDigits() that given a string, returns the sum of the digits 0-9 that appear in the string, ignoring all other characters.
Return 0 if there are no digits in the string.
(Note: Character.isDigit(char) tests if a char is one of the chars '0', '1', .. '9'. Integer.parseInt(string) converts a string to an int.)
Perform the following tests:
- sumDigits("aa1bc2d3") -----> 6
- sumDigits("aa11b33") -----> 8
- sumDigits("Chocolate") -----> 0
Practice problem 6
Write a method equalIsNot() that given a string, returns true if the number of appearances of "is"
anywhere in the string is equal to the number of appearances of "not" anywhere in the string (case sensitive).
Perform the following tests:
- equalIsNot("This is not") -----> false
- equalIsNot("This is notnot") -----> true
- equalIsNot("noisxxnotyynotxisi") -----> true
Practice problem 7
Write a method maxBlock() that given a string, returns the length of the largest
"block" in the string. A block is a run of adjacent chars that are the same.
Perform the following tests:
- maxBlock("hoopla") -----> 2
- maxBlock("abbCCCddBBBxx") -----> 3
- maxBlock("") -----> 0
Practice problem 8
Write a short program that reads a sentence .
Count the number of a's, b's, c's ... z's in the sentence.
Your output should look similar to the following:
Input: These French fries are cold.
Output:
a's = 1
c's = 2
e's = 5
f's = 2
h's = 2
n's = 1
r's = 3
s's = 2
t's = 1