CISC 101 Tutorial 7 - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CISC 101 Tutorial 7

Description:

public static String remCharFromString(String str, char c) { String ans ... if( temp.equals(String.valueOf(c)) == false ) ans = ans temp; return ans; ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 17
Provided by: protoco
Category:

less

Transcript and Presenter's Notes

Title: CISC 101 Tutorial 7


1
CISC 101 - Tutorial 7 Strings
Muhammad Aboelfotoh mha_at_cs.queensu.ca
2
Special syntax - don't need to use
new Example String name John Finding the
String variable (e.g. stringName)
length... stringName.length()
(method!) Finding the array variable (e.g.
arrayName) length... arrayName.length
(variable!) First location in the String (same
as arrays) start at index zero
3
Special treatment Strings are treated as if they
are primitive types Example public static void
modify(int myarrInt, String str, char
myarrChar) myarrInt0 7 str
"hi" myarrChar0'y' public static void
main(String args) int arrInt
1,2,3,4,5 String str "John" char
arrChar 'a','b','c','d'
modify(arrInt, str, arrChar)
System.out.println(arrInt0 "\n" str "\n"
arrChar0) System.out.println(str.length()
) System.out.println(arrInt.length) What
would the above code output to the screen?
4
Output 7 John y 4 5
5
Methods we can apply to String... Assuming... int
sIndex, eIndex, n String s1, s2, str, strA,
strB Concatenation strings may be appended to
each other e.g. str s1 s2 str1.subString(sIn
dex, eIndex) s1.equals(s2) s1.compareTo(s2) s1.
toLowerCase() s1.toUpperCase() strA.indexOf(strB
, n) strA.lastIndexOf(strB) s1.valueOf(p) //
where p is some primitive type value // e.g.
int, String, char (Note String.valueOf(p)
returns same result) Integer.parseInt(str) Double
.parseDouble(s1)
6
Write a method that - takes in a String
parameter - counts the number of times that the
upper or lowercase x ('X' or 'x') occurs in its
String parameter - returns the count.
7
public static int numOfX(String str) int
count0 String temp for(int i0
iltstr.length() i) temp
str.substring(i,i1) if(
"x".equals(temp.toLowerCase()) true) count
return count
8
Write a method that converts the even numbered
characters in its parameter String to UPPER CASE,
and returns the resulting String. Examples Meth
od takes in Method returns bold
BoLd ASDF
ASDF Arnold
ArNoLd
9
public static String evenNumdCharsToUpper(String
str) String ans "" for(int
i0iltstr.length()i) String temp
str.substring(i,i1) if(i20) ans
ans temp.toUpperCase() else ans ans
temp return ans
10
Assuming the following... String x
Smiles String y char z 's', 'a', 'y', '
', 'c', 'h', 'e', 'e', 's', 'e' What is the
output of each of the following calls? i)
z.length() ii) hello.length() iii)
(x.compareTo(smiles)) 0 iv)
x.indexOf(mil, 2) v) x Smiles
11
Output i) Syntax error. Should be z.length
10 ii) 5 iii) false iv) -1 To find mil,
index should be at most equal to 1. v) true
12
String x Smiles String y char z 's',
'a', 'y', ' ', 'c', 'h', 'e', 'e', 's',
'e' What is the output of the following
code? y x System.out.println(y) for (int
iz.length-1 igt0 i--) System.out.println(z
i) x jump System.out.println(x and
y) char a z z8 'z' for (int
iz.length-1 igt0 i--)
System.out.println(zi) System.out.println(
ai)
13
Output Smiles e s e e h c y a s
14
Output (contd.) jump and Smiles e e z z e e e e
h h c c y y a a s s
15
Write a method that - takes a String and a single
character String. - returns the original String
but with every occurence of the entered single
character removed. Example Method takes in
Method returns hello l
heo
16
public static String remCharFromString(String
str, char c) String ans "" for(int
i0 iltstr.length() i) String temp
str.substring(i,i1) if(
temp.equals(String.valueOf(c)) false ) ans
ans temp return ans
Write a Comment
User Comments (0)
About PowerShow.com