# Introduction to Computing: Lecture 16 Character Strings - PowerPoint PPT Presentation

PPT – Introduction to Computing: Lecture 16 Character Strings PowerPoint presentation | free to download - id: 674533-ZTdhN

The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
Title:

## Introduction to Computing: Lecture 16 Character Strings

Description:

### Introduction to Computing: Lecture 16 Character Strings Dr. Bekir KARLIK Yasar University Department of Computer Engineering bekir.karlik_at_yasar.edu.tr – PowerPoint PPT presentation

Number of Views:176
Avg rating:3.0/5.0
Slides: 46
Provided by: bekirk9
Category:
Tags:
Transcript and Presenter's Notes

Title: Introduction to Computing: Lecture 16 Character Strings

1
Introduction to Computing Lecture 16 Character
Strings
Dr. Bekir KARLIK Yasar University Department of
Computer Engineering bekir.karlik_at_yasar.edu.tr
2
Topics
• Strings
• Representation
• Declaration
• Functions
• Common mistakes
• Index of a char in a string

3
On the Nature of Strings
• Recall Main memory
• contiguous array of cells
• each cell has an address

0x1FFF
0x2000
0x2001
0x2002
0x1FFE
etc.
4
On 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
5
On 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
6
On 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
7
Character Strings
Declaration 1
char name5
Declaration 2
define MAXLENGTH 5 char nameMAXLENGTH
name is 0x2000
0x2000
0x2004
8
String 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
9
Character 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
10
Character Strings
Can store at most 4 letters, because of \0
Initialization
char name5 Ali
name is 0x2000
A
l
i
\0
0x2000
0x2004
11
Character Strings
Takes up an extra cell for \0
Declaration 3
char name Ali
name is 0x2000
A
l
i
\0
0x2000
0x2003
12
Character Strings
Result is undefined if you try to modify this
string.
Declaration 4
char name Ali
0x3000
A
l
i
\0
name
0x3000
0x3003
13
Character Strings
Declaration 5
char name
String with arbitrary length? No! Will cause an
error.
14
A Char in a String
• The size of a character string is fixed.
• Character at position index
• stringindex
• first character has index 0

15
A 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.
16
A 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)
17
A 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
18
String Operations
• include ltstring.hgt
• Operations
• Assignment strcpy()
• Concatenation strcat()
• Comparison strcmp()
• Length strlen()

19
String 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
20
String 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
21
String 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!
22
Common Mistake
Wrong Assignment
Example 1
char name15 Ali char name25
Sami name2 name1
Error LValue required....
23
Common Mistake
Example 2
char name1 Ali char name2
Sami name2 name1
Better avoid initialising strings this way.
(Usually, no error message.)
24
Common Mistake
char name1 Ali char name2 Sami
25
Common Mistake
name2 name1
0x2000
A
l
i
\0
name1
0x2000
0x2003
0x2000
S
a
m
i
\0
name2
0x3990
0x3994
26
Common Mistake
Not enough space
char name Ali strcpy(name, Samir)
name is 0x2000
A
l
i
\0
0x2000
0x2003
27
Common Mistake
Not enough space
Requires caution.
char name Ali strcpy(name, Samir)
name is 0x2000
S
a
m
i
r
\0
0x2000
0x2003
28
String 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
29
String 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
30
String 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
31
String 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
32
Common Mistake
Not enough space
char name5 strcpy(name, Ali) strcat(name,
Osman)
name is 0x2000
A
l
i
\0
0x2000
0x2004
33
Common 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
34
String 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
35
String 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
36
Common 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)
37
String Operation Length
char string1100 strcpy(string1,
Apple) printf(d\n, strlen(string1))
output 5
38
Common Mistake
Not enough space
char name5 strcpy(name, Bekir)
Dont forget the \0.
39
Character 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.

40
Example 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?")
printf("Who are you? ") scanf("s", user)
Greet(user) printf("s\n", user) return
0
41
Example hello3.c
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
42
Example hello3.c
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
43
Example hello3.c
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
44
More of scanf demystified
No ampersand () in scanf with strings!