0% found this document useful (0 votes)
39 views

Tutorial 07 - Tutorial

This document provides a tutorial on string and string processing in Java. It contains examples of string methods like length(), charAt(), indexOf(), substring(), replace(), replaceAll(), and toUpperCase(). It also provides code segments to demonstrate counting characters, reversing a string, extracting substrings, changing case, and determining gender based on the last digit of a 12-digit Malaysian identity number. Examples are provided for sample inputs like "University Kebangsaan Malaysia", "Star Wars", and "kebangsaan". Hands-on exercises are included to help learners practice using string methods.

Uploaded by

Farah Cakey
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Tutorial 07 - Tutorial

This document provides a tutorial on string and string processing in Java. It contains examples of string methods like length(), charAt(), indexOf(), substring(), replace(), replaceAll(), and toUpperCase(). It also provides code segments to demonstrate counting characters, reversing a string, extracting substrings, changing case, and determining gender based on the last digit of a 12-digit Malaysian identity number. Examples are provided for sample inputs like "University Kebangsaan Malaysia", "Star Wars", and "kebangsaan". Hands-on exercises are included to help learners practice using string methods.

Uploaded by

Farah Cakey
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

TUTORIAL: MODULE 7 (STRING & STRING PROCESSING)

Section A
1. String is a primitive data type. True or False?
2. Briefly describe the immutability of a string, and give one example:
String is not a primitive data type. False.

Section B: Hands Tracing


Consider the following declaration:
Scanner sc = new Scanner(System.in);

1. Using a string literal, write a program to reverse every character in the string.

String w = sc.nextLine();

for(int i=w.length(); i>0; i--) {

System.out.print(w.charAt(i-1));

2. What is the output of the following code segments if the input is “University Kebangsaan
Malaysia”?
String str = sc.nextLine();
System.out.print(str.charAt(str.length()-1));

Output : Universiti Kebangsaan Malaysia

3. What is the output of the following code segments if the input is “University Kebangsaan
Malaysia”?
String str = sc.next();
System.out.print(str.length()-1);

Output : 9 (because str.length is the length of the string.) But, I have a question for this (29).
4. What is the output of the following code segments if the input is “Star Wars”?
String str = sc.nextLine(); int
k = str.indexOf(' ');
String newstr = str.substring(0, k);
System.out.print(newstr);

Output : Star

5. What is the output of the following code segments if the input is “Star Wars”?
String str = sc.nextLine(); char
ch = str.charAt(1);
System.out.print(ch);

Output : t

6. What is the output of the following code segments if the input is “kebangsaan”? strOrig =
sc.nextLine();
String strNew = strOrig.replaceAll("[an]", "");
System.out.println(strNew);

kebgs

7. What is the output of the following code segments if the input is “kebangsaan”? strOrig =
sc.nextLine();
String strNew2 = strOrig.replace("b","B");
System.out.println(strNew2);

keBangsaan //it replace b with B.

Section C: Write Code Segments

1. Given the following declaration:


Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String str2 = new String();
Write code segments for the following tasks.

a. Print the length of the inputted string.

System.out.println(str.length());

b. Convert all characters in str to uppercase.

System.out.println(str.toUpperCase());

c. Count the total number of consonants in str.


str = str.toLowercase();

for(int i =0; i<str.length();i++) {

switch(str.charAt(i)) {

case 'a' : freq1++;


break;

case 'e' : freq1++;


break;

case 'i' : freq1++;


break;

case 'o' : freq1++;


break;

case 'u' : freq1++;


break;

case ' ' :


break;
default : freq2++;
}

}
System.out.println(freq2);

d. Count the total number of digits in str.


for(int i =0; i<str.length();i++) {

switch(str.charAt(i)) {

case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
case '0': freq1++;
break;
default : freq2++;
}

}
System.out.println(freq1);

e. Count the number of blank spaces in str.

for(int i =0; i<str.length();i++) {

switch(str.charAt(i)) {

case ' ' : freq1++;


break;

default : freq2++;
}

}
System.out.println(freq1);

f. Replace all character ‘a’ in str to ‘*’ and store the new string in str2.

for(int i =0; i<str.length();i++) {

str2 = str.replace("a", "*");


}

System.out.println(str2);

g. Remove all vowels in str and store the new string in str2.

for(int i =0; i<str.length();i++) {


str2 = str.replaceAll("[aeiou]", "");

}
System.out.println(str2);
}

2. Write code segments that reads 12-digit Mykad number and display the person's gender.
If the last digit is even number, then the person’s gender is female and otherwise is male.
For example, if the input is 971225561234 then the output is ‘Female’.

String m = sc.nextLine();

if(str.CharAt(m.length-1) % 2 ==0) {

System.out.println("Female");
}
else
System.out.println("Male");

If(str.CharAt(str.length-1) % 2 ==0){

You might also like