Title: Introduction to Computing: Lecture 16 Character Strings
1Introduction to Computing Lecture 16Character
Strings
Dr. Bekir KARLIK Yasar University Department of
Computer Engineering bekir.karlik_at_yasar.edu.tr
2Topics
- Strings
- Representation
- Declaration
- Functions
- Common mistakes
- Index of a char in a string
3On the Nature of Strings
- Recall Main memory
- contiguous array of cells
- each cell has an address
0x1FFF
0x2000
0x2001
0x2002
0x1FFE
etc.
4On the Nature of Strings (cont.)
- Recall Variable declaration
- sets aside a box to contain a value
Example
char ch
ch B
0x1FFF
0x2000
0x2001
0x2002
0x1FFE
etc.
B
5On the Nature of Strings (cont.)
- String declaration
- sets aside an array of cells
- each cell contains a char
- address of first cell in the array
Example
char name5
Specifies number of cells in the array
6On the Nature of Strings (cont.)
- String declaration
- sets aside an array of cells
- each cell contains a char
- address of first cell in the array
Example
char name5
0x2000
0x2004
7Character Strings
Declaration 1
char name5
Declaration 2
define MAXLENGTH 5 char nameMAXLENGTH
name is 0x2000
0x2000
0x2004
8String Input/Output
No ampersand ()!
include ltstdio.hgt define MAXLENGTH 15 int
main() char string1MAXLENGTH char
string2MAXLENGTH scanf("s s", string1,
string2) printf("s s\n", string1,
string2) return 0
9Character Strings
- Terminating Character
- Marks the end of string
- Special char \0
- aka NUL (single L)
Initialization
char name5 Ali
name is 0x2000
A
l
i
\0
0x2000
0x2004
10Character Strings
Can store at most 4 letters, because of \0
Initialization
char name5 Ali
name is 0x2000
A
l
i
\0
0x2000
0x2004
11Character Strings
Takes up an extra cell for \0
Declaration 3
char name Ali
name is 0x2000
A
l
i
\0
0x2000
0x2003
12Character Strings
Result is undefined if you try to modify this
string.
Declaration 4
char name Ali
0x3000
A
l
i
\0
name
0x3000
0x3003
13Character Strings
Declaration 5
char name
String with arbitrary length? No! Will cause an
error.
14A Char in a String
- The size of a character string is fixed.
- Character at position index
- stringindex
- first character has index 0
15A Char in a String
0x3995
0x399C
name is 0x3995
A
i
s
e
\0
index 0
index 4
char name8 Aise int i 2 printf(Char
at index d is c.\n, i, namei)
output Char at index 2 is h.
16A Char in a String
0x3995
0x399C
name is 0x3995
A
i
s
e
\0
index 2
char name8 Aise name2
X printf(Name s\n, name)
17A Char in a String
0x3995
0x399C
name is 0x3995
A
i
X
e
\0
index 2
char name8 Aise name2
X printf(Name s\n, name)
output Name AiXe
18String Operations
- include ltstring.hgt
- Operations
- Assignment strcpy()
- Concatenation strcat()
- Comparison strcmp()
- Length strlen()
19String Operation Assignment
include ltstdio.hgt include ltstring.hgt define
MAXLENGTH 100 int main() char
string1MAXLENGTH char string2MAXLENGTH
strcpy(string1, Hello World!)
strcpy(string2, string1) return 0
string1 ltgarbagegt string2 ltgarbagegt
20String Operation Assignment
include ltstdio.hgt include ltstring.hgt define
MAXLENGTH 100 int main() char
string1MAXLENGTH char string2MAXLENGTH
strcpy(string1, Hello World!)
strcpy(string2, string1) return 0
string1 Hello World! string2 ltgarbagegt
21String Operation Assignment
include ltstdio.hgt include ltstring.hgt define
MAXLENGTH 100 int main() char
string1MAXLENGTH char string2MAXLENGTH
strcpy(string1, Hello World!)
strcpy(string2, string1) return 0
string1 Hello World! string2 Hello World!
22Common Mistake
Wrong Assignment
Example 1
char name15 Ali char name25
Sami name2 name1
Error LValue required....
23Common Mistake
Bad Assignment
Example 2
char name1 Ali char name2
Sami name2 name1
Better avoid initialising strings this way.
(Usually, no error message.)
24Common Mistake
Bad Assignment
char name1 Ali char name2 Sami
25Common Mistake
Bad Assignment
name2 name1
0x2000
A
l
i
\0
name1
0x2000
0x2003
0x2000
S
a
m
i
\0
name2
0x3990
0x3994
26Common Mistake
Not enough space
char name Ali strcpy(name, Samir)
name is 0x2000
A
l
i
\0
0x2000
0x2003
27Common Mistake
Not enough space
Requires caution.
char name Ali strcpy(name, Samir)
name is 0x2000
S
a
m
i
r
\0
0x2000
0x2003
28String Operation Concatenation
char string1MAXLENGTH char string2MAXLENGTH
strcpy(string1, Goodbye) strcpy(string2, ,
Cruel ) strcat(string1, string2) strcat(string
1, string2) strcat(string1, World!)
string1 Goodbye string2 , Cruel
29String Operation Concatenation
char string1MAXLENGTH char string2MAXLENGTH
strcpy(string1, Goodbye) strcpy(string2, ,
Cruel ) strcat(string1, string2) strcat(string
1, string2) strcat(string1, World!)
string1 Goodbye, Cruel string2 , Cruel
30String Operation Concatenation
char string1MAXLENGTH char string2MAXLENGTH
strcpy(string1, Goodbye) strcpy(string2, ,
Cruel ) strcat(string1, string2) strcat(string
1, string2) strcat(string1, World!)
string1 Goodbye, Cruel , Cruel string2 ,
Cruel
31String Operation Concatenation
char string1MAXLENGTH char string2MAXLENGTH
strcpy(string1, Goodbye) strcpy(string2, ,
Cruel ) strcat(string1, string2) strcat(string
1, string2) strcat(string1, World!)
string1 Goodbye, Cruel , Cruel World! string2
, Cruel
32Common Mistake
Not enough space
char name5 strcpy(name, Ali) strcat(name,
Osman)
name is 0x2000
A
l
i
\0
0x2000
0x2004
33Common Mistake
Not enough space
char name5 strcpy(name, Ali) strcat(name,
Osman)
name is 0x2000
A
l
i
O
s
m
a
n
\0
0x2000
0x2004
34String Operation Comparison
strcpy(string1, Apple) strcpy(string2,
Wax) if (strcmp(string1, string2) lt 0)
printf(s s\n, string1, string2) else
printf(s s\n, string2, string1)
output Apple Wax
35String Operation Comparison
strcpy(string1, Apple) strcpy(string2,
Wax) if (strcmp(string1, string2) lt 0)
printf(s s\n, string1, string2) else
printf(s s\n, string2, string1)
Returns negative if string1 lt string2 zero if
string1 string2 positive if string1 gt string2
36Common Mistake
Wrong Comparison
strcpy(string1, Apple) strcpy(string2,
Wax) if (string1 lt string2) printf(s
s\n, string1, string2) else printf(s
s\n, string2, string1)
37String Operation Length
char string1100 strcpy(string1,
Apple) printf(d\n, strlen(string1))
output 5
38Common Mistake
Not enough space
char name5 strcpy(name, Bekir)
Dont forget the \0.
39Character Strings as Parameters
- Strings as formal parameters are declared as
char or char - Examples
- void Greet ( char name )
- void Greet ( char name )
- As pointer to the first element of the string
(array of chars). - Changes to the string inside the function affect
the actual string.
40Example hello3.c
include ltstdio.hgt include ltstring.hgt define
NAMELEN 50 / Print a simple greeting to the
user. / void Greet ( char name )
strcat(name,"! How are you?")
int main() char userNAMELEN
printf("Who are you? ") scanf("s", user)
Greet(user) printf("s\n", user) return
0
41Example hello3.c
int main() char userNAMELEN
printf("Who are you? ") scanf("s", user)
Greet(user) printf("s\n", user) return
0
include ltstdio.hgt include ltstring.hgt define
NAMELEN 50 / Print a simple greeting to the
user. / void Greet ( char name )
strcat(name,"! How are you?")
name
user
Bekir\0
42Example hello3.c
int main() char userNAMELEN
printf("Who are you? ") scanf("s", user)
Greet(user) printf("s\n", user) return
0
include ltstdio.hgt include ltstring.hgt define
NAMELEN 50 / Print a simple greeting to the
user. / void Greet ( char name )
strcat(name,"! How are you?")
name
user
Bekir! How are you?\0
43Example hello3.c
int main() char userNAMELEN
printf("Who are you? ") scanf("s", user)
Greet(user) printf("s\n", user) return
0
include ltstdio.hgt include ltstring.hgt define
NAMELEN 50 / Print a simple greeting to the
user. / void Greet ( char name )
strcat(name,"! How are you?")
user
Bekir! How are you?\0
44More of scanf demystified
No ampersand () in scanf with strings!
int main() char userNAMELEN
printf("Who are you? ") scanf("s", user)
Greet(user) printf("s\n", user) return
0
45Summary
- A string is a contiguous array of chars
- The string identifier is the address of the first
char in the string - Individual chars are accessed using the
strindex notation - There are C library functions for copying,
concatenating and comparing strings.