Topic 6 - Sample Questions - Strings (6 days)


1. Write a method that will print the name in reverse order.

public void printReverse()


2. Write a method that will print out your first name only.

public void printFirst(String na)


3. Write a method that will print out your last name only.


public void printLast(String na)

4. Write a method that will print out every other letter in the String.

public void printEveryOther(String na)


5. Write a method that will print every other letter in reverse order.

public void printReverse2(String n)


6. Write a method that will receive a letter and will return how many times that letter was in the String.

public int countLetters(String name,String let)


7. Write a method that will print out the sentence without any vowels.

public void printNoVowels(String n)


8. Write a method will print the word in a right triangle.

public void printTriangle(String n)


EX. word = tom

t

to

tom


9. Write a method that will return the first name.

public String getFirst(String na)


10. Write a method that will return how many words were in the sentence.

public int countWords(String n)


11. Write a method that will print your last name the number of letters that are in your first name. For example if your name was Lhens Vilson the computer would print out Vilson 5 times (because there are 5 letters in Lhens).

public void printLast(String n)

12. Write a method that will print the first letter of the first name, followed by the first letter of the last name.
Example :
if the name was Joe Smith J S would come out.

public void printName2(String n)

13. Write a method that will return a new string that is the same as the first except all spaces are removed (hard)
Example :
original sentence : Mary had a little lamb.
Maryhadalittlelamb is returned

public String removeSpaces(String word)

14. Write a method that will return the last name.

public String getLast(String n)



15. Write a method that will print out the last name followed by the first name. You must call the functions written in #9 and #14 to get credit.

public void printNameSwitched(String n)



16. Write a method that will return the last name followed by the first name. You must call the functions written in #9 and #14 to get credit.

For example : If name was "Joe Smith" "Smith Joe" would be returned

public String returnNameSwitched(String n)



17. Write a method that will replace all occurrences of a letter in the String with another letter.
For example : if our original string was Joe Jones and let1 = "o" and let = "a" then s would become "Jae Janes"

public void replace (String word,String let1, String let2)

18. Same as above except return a String with the changes. Do not disturb the first String.
For example : if our original string was Joe Jones and let1 = "o" and let = "a" then "Jae Janes" would be returned

public String replace (String word, String let1, String let2)

19. Write a program that will let you enter a sentence and then a word from the sentence and then another word and will then replace the first word in the sentence with the second.

Example


Enter a sentence Ramon is a nice young man.
Enter a word young
Enter a word old
New sentence is
Ramon is a nice old man.

20. Write a method that would return the number of vowels in a String.
public int ctVowels(String n)

21. Write a method that will print out the first letter in all the words in a sentence.
public void printFirstLetters(String n)

22. Write a method that will print each word in a sentence on a line by itself.

public void printWords(String sentence)

23. Write a method that will return how many letters there are in the last name.

public int lettersInLastName(String name)

24. Write a method that will remove all occurences of a particular letter from a String and then return that new String.

public String removeLetter(String n,String let)

25. Write a program that will print the first name in reverse.

26. Write a program that will let you enter a full name (with a middle name) and will then print out only the middle name.

27. Write a program that will replace the first occurence of an "e" with an "a" in a String. If there is no "e" then don't do anything.