Title: Computer Programming
1Computer Programming
2Objective
- To be able use the string processing functions of
the string handling library (string.h)
3Outlines
- String Array
- String Operations
4String Array
- A character string must have the terminating
character (\0)
5Strings Array
Declaration 1
char name5
Declaration 2
define MAXLENGTH 5 char nameMAXLENGTH
name
0
4
6Character String Declaration
Can store at most 4 letters, because of \0
- Terminating Character
- Marks the end of string
- Special char \0z
char name5 Ann
7Character String Declaration (cont)
char name
String with arbitrary length? No! Will cause an
error
Size of name is unknown or zero
8A Char in a String (cont)
char name8 John int i 2 printf(Char
at index d is c.\n, i, namei)
output Char at index 2 is h.
9A Char in a String (cont)
X
char name8 John name2
X printf(Name s\n, name)
10A Char in a String (cont)
name
J
o
X
n
\0
index 2
char name8 John name2
X printf(Name s\n, name)
output Name JoXn
11char name3 John
char name4 John
char name5 John
Garbage data
12C Errors to Avoid
no range checking
C has no range checking, so if your index past
the end of the array, it will not tell you about
it. It will eventually crash or give you garbage
data.
13Example
- include ltstdio.hgt
- main()
-
- char word20
- word0 'H'
- word1 'e'
- word2 'l'
- word3 'l'
- word4 'o'
- word5 0
- printf("The contents of word is --gts\n",
word ) -
14Example (ch)
include ltstdio.hgt include ltconio.hgt main (
) char a4 D', 'A', R', K' int
i for(i0ilt3i)
printf("c\t",ai) printf("\n")
getch () return(0)
15Example (ch)
include ltstdio.hgt include ltconio.hgt void
expression(void) main( ) char a4 'G',
'A', 'M', 'E' int i for(i0ilt3i)
printf("c\t",ai) printf("\n")
expression() return(0) void
expression (void) printf("O\tV\tE\tR")
16String Operations
- include ltstring.hgt
- Operations
- Assignment strcpy()
- Concatenation strcat()
- Length strlen()
17String Operation Assignment
include ltstdio.hgt include ltstring.hgt define
MAXLENGTH 100 int main() char
string1MAXLENGTH char string2MAXLENGTH
strcpy(string1, Hello World!)
strcpy(string2, string1) printf("s\n s\n",
string1, string2) return 0
18String Operation Assignment (cont)
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
19String Operation Assignment (cont)
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!
20Common Mistake 1
Incompatible types
Example
char name15 Ann char name25
Dave name2 name1
Error LValue required ...
21String 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
22String Operation Concatenation (cont)
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
23String Operation Concatenation (cont)
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
24String Operation Concatenation (cont)
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
25String Operation Length
char string1100 strcpy(string1,
Apple) printf(d\n, strlen(string1))
output 5
Number of char-s before the \0
26Example
include ltstdio.hgt include ltstring.hgt include
ltconio.hgt main ( ) char name
strcpy(name,Siti) printf( My name is
c\n, name) strlen(name) return(0)
Compile result My name is Siti 4