Upload
basia-oneill
View
35
Download
1
Embed Size (px)
DESCRIPTION
Strings : Getting Stuff Done. Basic Recipes. Does this look like an email address?. Basic Recipes. Does this look like an email address? Is there an @. What is that Warning???. Ugly digression…. Basic Recipes. Does this string have a @ in it? Happier compiler version. - PowerPoint PPT Presentation
Citation preview
Strings : Getting Stuff Done
Basic Recipes
• Does this look like an email address?
0 1 2 3 4 5 6 7 8
b o b @ a . c o m
Basic Recipes
• Does this look like an email address?– Is there an @
0 1 2 3 4 5 6 7 8
b o b @ a . c o m
What is that Warning???
• Ugly digression…
Basic Recipes
• Does this string have a @ in it?– Happier compiler version
0 1 2 3 4 5 6 7 8
b o b @ a . c o m
Char vs 1 char string
• 'a' is not the same kind of thing as "a"
Number Object
Char vs 1 char string
• string.at(index) and string[index] give a char
• No
• Yes
Char vs 1 char string
• All other string functions give string
• Yes
• No
Basic Recipes
• Want to separate username and domain:
0 1 2 3 4 5 6 7 8
b o b @ a . c o m
Basic Recipes
• Method 2:
0 1 2 3 4 5 6 7 8
b o b @ a . c o m
Basic Recipes
• I want to replace a @ with #:
0 1 2 3 4 5 6 7 8
b o b @ a . c o m
Basic Recipes
• I want to replace a @ with #:– Shorter version
0 1 2 3 4 5 6 7 8
b o b @ a . c o m
Set one char in stringto new value… must be a char
Basic Recipes
• I want to replace a @ with #:– Long version
0 1 2 3 4 5 6 7 8
b o b @ a . c o m
Chopping
• Generally easier to chop as you go:
Chopping
• Finding both '-' and then chopping:
Looping
• Finding all groups in string like:123-4324-23-34-…-234
• While there is another dash, find dash, print everything up to dash
Looping
• By chopping parts off:
Looping
• By moving start/end indexes:
Looping Characters
• Process each character : – Loop from 0 to length - 1
0 1 2 3 4 5 6 7 8
b o b @ a . c o m
Current positionCurrent letter
Looping Characters
• Backwards
0 1 2 3 4 5 6 7 8
b o b @ a . c o m
Modifying as we go
• Shift all chars by one:– Get current letter–Modify– Replace current letter with changed
Case Study Hex Conversion
• Convert hex numbers to decimal:– Possible digits:
0-9, A (10), B (11), C (12), D (13), E (14), F (15)– Place values powers of 16:
1 x 4096 + 2 x 256 + 14 x 16 + 9 x 1 = 484112E916 = 484110
4096’s163
256’s162
16’s161
1’s160
1 2 E 9
Case Study Hex Conversion
• Need to access each digit• Need to figure out its value• Need to keep track of what place we are in• Need to build up a total
4096’s163
256’s162
16’s161
1’s160
1 2 E 9
Case Study Hex Conversion
• Need to access each digit• Need to figure out its value– char '4' != 4– But '4' – '0' = 4
Case Study Hex Conversion
• Need to access each digit• Need to figure out its value• Need to keep track of what place we are in– Each column is 16 x bigger than one to right
• Need to build up a total
4096’s163
256’s162
16’s161
1’s160
1 2 E 9