Intermediate C Programming nd
(2 )
Yung-Hsiang Lu, Purdue University
Geroge K. Thiruvathukal, Loyola University Chicago
CRC Press
1
ISBN 9781032189819
Chapter 06 Strings
2
Count Occurrences of a Word
an article (in a file)
count (a number)
+ a word
3
Understand C Strings
• C has no “string” data type.
• C uses “array of characters + \0” as a string
• Each element can store a value between 0 and 255
• Conversion between numbers and characters based on ASCII
E C E P U R D U E \0
Don’t Care
(after \0)
2 6 4 C \0 DC DC DC DC DC
C P R O G R \n \0 DC
4
String Functions
5
String Functions
pay attention to the order
6
C array is always a pointer
char * arr1;
arr1 = malloc(sizeof(char) * 20);
// arr1 stores the address of the first element
strcpy(arr1, “Purdue ECE”); Double quotation
char arr2[20]; automatically add ‘\0’
// arr2 is equivalent to & arr2[0],
// i.e., address of the first element
// cannot free (arr2)
free (arr1);
7
A pointer may not be an array
char ch = ‘A’;
char * p;
p = & ch; // a pointer, but there is no array
8
strcpy, not overlap
char s[20];
strcpy(s, “ECE Purdue”);
char * src = & s[0];
char * dest = & s[8];
9
char s[20];
strcpy(s, “ECE Purdue”);
char * src = & s[0];
char * dest = & s[8];
symbol s[0] s[8] s[19] src dest
address 100 108 119 120 128
value E C E P u r d u e \0 A100 A108
10
char s[20];
strcpy(s, “ECE Purdue”);
char * src = & s[0];
char * dest = & s[8];
strcpy(dest, src);
symbol s[0] s[8] s[19] src dest
address 100 108 119 120 128
value E C E P u r d u e \0 A100 A108
E C E
11
String Functions
12
const in argument
13
14
this is ok
this is not allowed (cannot use the LHS rule)
15
Frame Symbol Address Value
func t 212 123
b 208 A104
a 200 A100
main y 104 -456
x 100 123
16
Frame Symbol Address Value
func t 212 123
b 208 A104
a 200 A100
main y 104 -456
x 100 123
-456
17
Frame Symbol Address Value
func t 212 123
b 208 A100
a 200 A100
main y 104 -456
x 100 -456
18
19
20
21
22
‘\0’ in string
• The array must have space to store this special character
• strelen does not count it
without + 1,
char * mystrdup(const char * src) program behavior undefined
{
char * p = malloc(sizeof(char) * (strlen(src) + 1));
strcpy(p, src);
return p; for ‘\0’
}
23
Symbol Address Value
p 222 U
strstr t 214 A200
213 \0
212 E
char *t = “PCE ECECECECE”;
211 C
// How many ECE does t have? 210 E
// Does “ECECE” count as one or two? 209 C
208 E
char * p; 207 C
p = strstr(t, “ECE”); 206 E
205 C
204 E
203
t[2] 202 E
t[1] 201 C
t[0] 200 P
24
Symbol Address Value
p 222 A204
strstr t 214 A200
213 \0
212 E
char *t = “PCE ECECECECE”;
211 C
// How many ECE does t have? 210 E
// Does “ECECE” count as one or two? 209 C
208 E
char * p; 207 C
p = strstr(t, “ECE”); 206 E
205 C
204 E
203
t[2] 202 E
t[1] 201 C
t[0] 200 P
25
Symbol Address Value
p 222 A204
strstr t 214 A200
213 \0
212 E
char *t = “PCE ECECECECE”;
211 C
// How many ECE does t have? 210 E
// Does “ECECE” count as one or two? 209 C
208 E
char * p; 207 C
p = strstr(t, “ECE”); 206 E
p = strstr(t, “ECE”); // p is 204 205 C
204 E
203
t[2] 202 E
t[1] 201 C
t[0] 200 P
26
Symbol Address Value
p 222 A204
strstr t 214 A200
213 \0
212 E
char *t = “PCE ECECECECE”;
211 C
// How many ECE does t have? 210 E
// Does “ECECE” count as one or two? 209 C
208 E
char * p; 207 C
p = strstr(t, “ECE”); 206 E
p = strstr(p, “ECE”); // p is 204 205 C
204 E
203
t[2] 202 E
t[1] 201 C
t[0] 200 P
27
Symbol Address Value
p 222 A205
strstr t 214 A200
213 \0
212 E
char *t = “PCE ECECECECE”;
211 C
// How many ECE does t have? 210 E
// Does “ECECE” count as one or two? 209 C
208 E
char * p; 207 C
p = strstr(t, “ECE”); 206 E
p ++; 205 C
204 E
203
t[2] 202 E
t[1] 201 C
t[0] 200 P
28
Symbol Address Value
q 230 A205
strstr p
t
222
214
A206
A200
213 \0
char *t = “PCE ECECECECE”; 212 E
211 C
// How many ECE does t have?
210 E
// Does “ECECE” count as one or two? 209 C
char * p; 208 E
207 C
p = strstr(t, “ECE”);
206 E
p ++; 205 C
char * q = p; 204 E
203
t[2] 202 E
t[1] 201 C
t[0] 200 29 P
Symbol Address Value
q 230 A205
strstr p
t
222
214
A206
A200
213 \0
char *t = “PCE ECECECECE”; 212 E
// How many ECE does t have? 211 C
210 E
// Does “ECECE” count as one or two? 209 C
char * p; 208 E
207 C
p = strstr(t, “ECE”);
206 E
p ++; 205 C
char * q = p; 204 E
203
p = strstr(q, “ECE”); // not t
t[2] 202 E
t[1] 201 C
t[0] 200 30
P
Symbol Address Value
char *t = “PCE ECECECECE”; q 230 A207
// How many ECE does t have? p 222 A207
t 214 A207
// Does “ECECE” count as one or two? 213 \0
char * p; 212 E
211 C
p = strstr(t, “ECE”);
210 E
p ++; 209 C
char * q = p; 208 E
207 C
p = strstr(q, “ECE”); // not t
206 E
p ++; 205 C
q = p; 204 E
203
t[2] 202 E
t[1] 201 C
t[0] 200 31
P
Symbol Address Value
char *t = “PCE ECECECECE”; q 230 A207
// How many ECE does t have? p 222 A208
t 214 A200
// Does “ECECE” count as one or two? 213 \0
char * p; 212 E
p = strstr(t, “ECE”); 211 C
210 E
p ++; 209 C
char * q = p; 208 E
207 C
p = strstr(q, “ECE”); // not t
206 E
p ++; 205 C
q = p; 204 E
203
p = strstr(q, “ECE”); t[2] 202 E
t[1] 201 C
32
t[0] 200 P
Symbol Address Value
char *t = “PCE ECECECECE”;
p 222 A204
// How many ECE does t have? t 214 A200
// Does “ECECE” count as one or two? 213 \0
212 E
char * p; 211 C
p = strstr(t, “ECE”); 210 E
209 C
208 E
207 C
206 E
205 C
204 E
203
t[2] 202 E
t[1] 201 C
t[0] 200 P
33
Symbol Address Value
char *t = “PCE ECECECECE”;
p 222 A207
// How many ECE does t have? t 214 A200
// Does “ECECE” count as one or two? 213 \0
212 E
char * p; 211 C
p = strstr(t, “ECE”); 210 E
p += strlen(“ECE”); 209 C
208 E
207 C
206 E
205 C
204 E
203
t[2] 202 E
t[1] 201 C
t[0] 200 P
34
Symbol Address Value
char *t = “PCE ECECECECE”; q 230 A207
// How many ECE does t have? p 222 A207
t 214 A200
// Does “ECECE” count as one or two? 213 \0
char * p; 212 E
211 C
p = strstr(t, “ECE”);
210 E
p += strlen(“ECE”); 209 C
char * q = p; 208 E
207 C
206 E
205 C
204 E
203
t[2] 202 E
t[1] 201 C
t[0] 200 35 P
Symbol Address Value
char *t = “PCE ECECECECE”; q 230 A207
// How many ECE does t have? p 222 A208
t 214 A200
// Does “ECECE” count as one or two? 213 \0
char * p; 212 E
p = strstr(t, “ECE”); 211 C
210 E
p += strlen(“ECE”); 209 C
char * q = p; 208 E
207 C
p = strstr(q, “ECE”);
206 E
205 C
204 E
203
t[2] 202 E
t[1] 201 C
36
t[0] 200 P
Symbol Address Value
char *t = “PCE ECECECECE”; q 230 A207
char * p; p 222 A208
t 214 A200
p = strstr(t, “ECE”); 213 \0
p += strlen(“ECE”); 212 E
char * q = p; 211 C
210 E
p = strstr(q, “ECE”); 209 C
// How many ECE does t have? 208 E
207 C
// Does “ECECE” count as one or two?
206 E
// p += strlen(“ECE”) count as one 205 C
// p ++ count as two 204 E
203
t[2] 202 E
t[1] 201 C
37
t[0] 200 P