You are on page 1of 3

SCE/CE9001/CM101/Tutorial8

Tutorial 8
Chapter 9: Strings
1. The following unknown1() method receives a string argument and a character
argument, and returns an integer value. escri!e the "ur"ose of the method. #ive an
e$am"le to su""ort %our answer.
public static int unknown1(String str, char c)
{
int i, y=0, z=0;
char[] tep = new char[str!length()];
"or (i=0; i # str!length(); i$$)
{
i"(str!char%t(i) &= c)
tep[y$$] = str!char%t(i);
else
z$$;
'
"or (i=0; i#y; i$$)
Syste!out!print(tep[i]);
return z;
'
&ns'
The function removes char from the string to form a new string. &t the same time, the
num!er of characters in the string is returned to the calling function. (or e$am"le, if the
string str ) *This is a string.* and char ) +s+, then after e$ecuting the function, str !ecomes
*Thi i a tring* and , ) - is returned.
.. /se the in"ut *S(%)* to trace the wor0ing of the following unknown*() method.
public static +oi, unknown*(String str)
{
char [] tep = new char[str!length()];
"or (int i=0; i#str!length(); i$$)
{
tep[i] = str!char%t(str!length()-1-i);
Syste!out!print(tep[i]);
'
'
&ns' 1f the in"ut is *ST&2*, then the out"ut of str is *2&TS*.
-. Consider the following unknown.() method. State the "ur"ose of the method. /se the
string */ow are you0* as the in"ut to trace the wor0ing of the method, and determine
the out"ut of the function.
public static +oi, unknown.(String str1, String1u""er str*)
{
int i, isspace = 1;
"or (i=0; i # str1!length(); i$$)
{
i" (isspace == 1)
1
SCE/CE9001/CM101/Tutorial8
{
str*!insert(0, str1!char%t(i));
isspace = 0;
'
i" (str1!char%t(i)== 2 2)
isspace = 1;
'
Syste!out!println(str*!toString());
'
&ns'
The un0nown method constructs the word formed !% the first characters of the words in
reverse order from the string str1. The word constructed should !e stored in the string
str.. Moreover, an% two words in str1 are assumed to !e se"arated !% a s"ace character.
1f the in"ut string is *3ow are %ou4*, then the out"ut string is *%a3*.
5. Show the out"ut of the following code.
public class (34 {
public static +oi, ain(String[] args){
String s = 5/i, 6oo, 7orning5;
Syste!out!println((s));
'
public static int (String s){
int count = 0;
"or (int i = 0; i #s!length(); i$$)
i" (8haracter!is9pper8ase(s!char%t(i)))
count$$;

return count;
'
'
&ns'
-
6. Show the out"ut of the following "rogram when invo0ed using
1. 7ava T86 1 have a dream
.. 7ava T86 81 . -9
-. 7ava T86
public class (3:{
public static +oi, ain(String[] args){
Syste!out!println(5;uber o" string is 5 $
args!length);
"or (int i = 0; i # args!length; i$$)
Syste!out!println(args[i]);
'
'
.
SCE/CE9001/CM101/Tutorial8
&ns'
1
*
.
-

You might also like