created 11/05/2010
Write a method (similar to Xlength()) that counts
the number of times a particular character occurs in a string.
The string and the particular character are both parameters of the method.
For example,
countChar( "XaaaYaaaZaaaYaaaaY", 'Y' )
returns 3.
Click here to go back to the main menu.
Write a method that creates a string that is similar to the argument string but with all the vowels removed. Regard a, e, i, o, u (upper and lower case) as vowels. For example,
removeVowels( "counterrevolutionaries" )
returns "cntrrvltnrs" .
removeVowels( "AUDIOBOOK" )
returns "DBK" .
Click here to go back to the main menu.
Write a program that implements string matching, except now the character '?' in either string matches any single cahracter at the same position in the other string. If both strings have '?' in the same position, the characters match each other. For example,
MOON matches M??N W?zar? matches ?izard ??? matches ??? ??????snake matches rattle?????
Click here to go back to the main menu.
Modify the recursive definition of equals so that two strings are equalX if both strings have the same number of X characters and those characters are in all the same positions. The strings may be different lengths, but the longer string can have no Xs in its extra length. For example,
equalX( "X", "X" ) is true equalX( "aaaXaaaX", "abcXcbaX" ) is true equalX( "XaXbXcX", "XtXoXpXdef" ) is true equalX( "XaXbXcX", "XtXoXpXdXf" ) is false equalX( "XXXX", "XX" ) is false equalX( "aXaXbXcX", "XtXoXpX" ) is false
Click here to go back to the main menu.
Write a complete program that tests the equals() method at the end
of the chapter.
Prompt the user for the two strings to be tested.
Make equals() a static method in the class that holds main().
Another implementation: define a class called myString that contains
a reference to a String and the method equals().
Your
main()
will create two myString objects and use their equals() methods.
Click here to go back to the main menu.
Change the recursive definition of equals so that vowels are ignored.
Now "kangaroo" equals "kongeroo", both
of them equals "kaangaro", and also equal "kngr".
The easy way to program this would be to first strip out all vowels and then compare what is left. But for this exercise, try to write a recursive definition that does the comparison on the unaltered strings. This may be somewhat difficult. Translate your definition into Java and test it.
Click here to go back to the main menu.
A palindrome is a string that is the same when reversed. For example, "abba" is a palindrome. Here is a math-like definition:
palindrome( "" ) = true
palindrome( x ) = true
palindrome( x+X+y ) = false, if x != y
= palindrome( X ), if x == y
The symbol x stands for a single character,
as does y.
The symbol X stands for a string of characters.
The symbol + stands for concatenation.
Implement palindrome() and a program that tests it.
Click here to go back to the main menu.
The previous program for palindrome required that each character of the string have a matching character in the same position but starting from the other end. But most popular palindromes involve punctuation, spaces, and capitalization that is ignored. So, for instance, the following are all palindromes under this extended definition:
Ah! Satan sees Natasha. Eva, can I see bees in a cave? Was it a car or a cat I saw?
Write a math-like definition for this extended idea of palindrome, and write a program that implements and tests it. You will need to add two more recursive cases to the definition of palindrome.
You will find the methods isLetterOrDigit() and toLowerCase()
from the class Character very useful.
(This program might prove to be difficult.)
Click here to go back to the main menu.