Computer Programming - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Computer Programming

Description:

Example (ch) String Operations #include string.h Operations: Assignment: strcpy ... %cn', name); strlen(name); return(0); Compile result: My name is Siti. 4 ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 27
Provided by: Owne8
Category:

less

Transcript and Presenter's Notes

Title: Computer Programming


1
Computer Programming
  • Chapter 7
  • String

2
Objective
  • To be able use the string processing functions of
    the string handling library (string.h)

3
Outlines
  • String Array
  • String Operations

4
String Array
  • A character string must have the terminating
    character (\0)

5
Strings Array
Declaration 1
char name5
Declaration 2
define MAXLENGTH 5 char nameMAXLENGTH
name
0
4
6
Character String Declaration
Can store at most 4 letters, because of \0
  • Terminating Character
  • Marks the end of string
  • Special char \0z

char name5 Ann
7
Character String Declaration (cont)
char name
String with arbitrary length? No! Will cause an
error
Size of name is unknown or zero
8
A 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.
9
A Char in a String (cont)
X
char name8 John name2
X printf(Name s\n, name)
10
A 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
11
char name3 John
char name4 John
char name5 John
Garbage data
12
C 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.
13
Example
  • 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 )

14
Example (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)
15
Example (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")
16
String Operations
  • include ltstring.hgt
  • Operations
  • Assignment strcpy()
  • Concatenation strcat()
  • Length strlen()

17
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) printf("s\n s\n",
string1, string2) return 0
18
String 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
19
String 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!
20
Common Mistake 1
Incompatible types
Example
char name15 Ann char name25
Dave name2 name1
Error LValue required ...
21
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
22
String 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
23
String 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
24
String 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
25
String Operation Length
char string1100 strcpy(string1,
Apple) printf(d\n, strlen(string1))
output 5
Number of char-s before the \0
26
Example
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
Write a Comment
User Comments (0)
About PowerShow.com