0% found this document useful (0 votes)
57 views2 pages

Practical 2 - Strings PDF

Uploaded by

Darian Chetty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views2 pages

Practical 2 - Strings PDF

Uploaded by

Darian Chetty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

COMP102: Computer Programming

Practical 2: Strings and Methods

Monday, 30 August 2021

Question 1: Equals to Ignoring Case


Java contains a method available to all String objects called
equalsIgnoreCase(String s). This method determines whether two strings match
without paying any attention to case. For example, “SIBONELO”, “Sibonelo”, and
“sIbOnElO” are all considered matching strings.

Write your own version of this method, which will have the following signature:

public static boolean equalsIgnoreCase(String one, String two).

This means that it will receive two strings as input and return a value of “true” if the
strings are equal, and “false” if they are not.

Use the main method to prompt the user for two strings. After checking if they match
or not, communicate the result to them on the console.

Question 2: Get Birthday


The South African ID number encodes someone’s birthday in the first six digits as
follows:
YYMMDD XXXX XX X.

The first two digits give the last two digits of the year a person was born. The next two
give the moth the person was born (01 for January – 12 for December). Finally, the
last two digits are the day the person was born.

Write a method called displayBirthday(String idNumber) that will receive an


ID number as a string. The method should then display the birthday as the day of
birth, month of birth in full, and the year of birth. For example, if the ID number
“200312 5478 08 2” is entered, the method will display:

“You were born on: 12 March 2020”.

Write a main method that will prompt the user to enter their ID number and then use
the displayBirthday(String idNumber) method to display their birthday on the
console.

1
Question 3: How Many?
Write a method howMany(String s, char c) that takes a string and a character
as input and returns the number of times the character occurs in the string. Now use
this method in the main method to count the number of vowels in a string entered
by a user.

Question 4: Shuffle
Write a method shuffle(String s) that receives a string as input and returns that
string with all of the characters mixed up. For example, given the string “Hello”, is
may return “eHoll” or “loHel”.

Test this message by prompting a user in the main method to enter a string to be
garbled. Shuffle this string and display the result to the user.

Question 5: Anagram
Write a method that checks whether two words are anagrams. Two words are
anagrams if they contain the same letters in any order. For example, “silent” and
“listen” are anagrams. The header of the method should be:

public static boolean isAnagram(String one, String two).

Write a test program that prompts the user to enter two strings and tells them
whether the two strings are anagrams or not.

You might also like