61CSE103
Programming 1
Lecture 8: Working with String and Character in C
INSTRUCTOR: Dr. Vuong Quoc Bao
Instructor: V. Q. Bao 61CSE103 Programming 1 1
Learning Objectives:
Instructor: V. Q. Bao 61CSE103 Programming 1 2
1. Fundamentals of Strings and Characters
Instructor: V. Q. Bao Programming 1 3
Instructor: V. Q. Bao 61CSE103 Programming 1 4
It is important to note the difference between the “seventh
element of the array” and“array element seven”. Because array
subscripts begin at 0, the“ seventh element of the array” has a
subscript of 6, while “ array element seven” has a subscript of 7
and is actually the eighth element of the array. This is a
source of“ off by-one” errors.
Instructor: V. Q. Bao 61CSE103 Programming 1 5
• When storing a string of characters in a character array, be
sure that the array is large enough to hold the largest
string that will be stored. C allows strings of any length to
be stored. If a string is longer than the character array in
which it is to be stored, characters beyond the end of the
array will overwrite data in memory following the array.
Instructor: V. Q. Bao 61CSE103 Programming 1 6
Instructor: V. Q. Bao 61CSE103 Programming 1 7
• Processing a single character as a string. A string is a
pointer—probably a respectably large integer. However, a
character is a small integer (ASCII values range 0–255). On
many systems this causes an error, because low memory
addresses are reserved for special purposes such as
operating-system interrupt handlers—so “access violations”
occur.
• Passing a character as an argument to a function when a
string is expected is a syntax error.
• Passing a string as an argument to a function when a
character is expected is a syntax error.
Instructor: V. Q. Bao 61CSE103 Programming 1 8
2. Character Handling Library
Instructor: V. Q. Bao Programming 1 9
2. Character Handling Library
Instructor: V. Q. Bao Programming 1 10
2. Character Handling Library
Instructor: V. Q. Bao Programming 1 11
Example 1
Instructor: V. Q. Bao 61CSE103 Programming 1 12
Example 1
Instructor: V. Q. Bao 61CSE103 Programming 1 13
Example 1
Instructor: V. Q. Bao 61CSE103 Programming 1 14
Example 2
Instructor: V. Q. Bao 61CSE103 Programming 1 15
Example 2
Instructor: V. Q. Bao 61CSE103 Programming 1 16
Example 3
Instructor: V. Q. Bao 61CSE103 Programming 1 17
Example 3
Instructor: V. Q. Bao 61CSE103 Programming 1 18
Instructor: V. Q. Bao 61CSE103 Programming 1 19
3. String-Conversion Functions
Instructor: V. Q. Bao Programming 1 20
Example 4
Instructor: V. Q. Bao 61CSE103 Programming 1 21
Example 5
Instructor: V. Q. Bao 61CSE103 Programming 1 22
Example 6
Instructor: V. Q. Bao 61CSE103 Programming 1 23
Example 7
Instructor: V. Q. Bao 61CSE103 Programming 1 24
Example 8
Instructor: V. Q. Bao 61CSE103 Programming 1 25
Example 9
Instructor: V. Q. Bao 61CSE103 Programming 1 26
4. Standard Input/Output Library Functions
Instructor: V. Q. Bao Programming 1 27
Example 10
Instructor: V. Q. Bao 61CSE103 Programming 1 28
Example 10
Instructor: V. Q. Bao 61CSE103 Programming 1 29
Example 11
Instructor: V. Q. Bao 61CSE103 Programming 1 30
Example 11
Instructor: V. Q. Bao 61CSE103 Programming 1 31
Example 12
Instructor: V. Q. Bao 61CSE103 Programming 1 32
Example 13
Instructor: V. Q. Bao 61CSE103 Programming 1 33
5. String Manipulation Functions of the String Handling Library
Instructor: V. Q. Bao Programming 1 34
Example 14
Instructor: V. Q. Bao 61CSE103 Programming 1 35
Example 15
Instructor: V. Q. Bao 61CSE103 Programming 1 36
Example 15
Instructor: V. Q. Bao 61CSE103 Programming 1 37
6. Comparison Functions of the String- Handling Library
Instructor: V. Q. Bao Programming 1 38
Example 16
Instructor: V. Q. Bao 61CSE103 Programming 1 39
Example 16
Instructor: V. Q. Bao 61CSE103 Programming 1 40
Instructor: V. Q. Bao 61CSE103 Programming 1 41
Instructor: V. Q. Bao 61CSE103 Programming 1 42
Example 17
Instructor: V. Q. Bao 61CSE103 Programming 1 43
Example 17
Instructor: V. Q. Bao 61CSE103 Programming 1 44
Example 18
Instructor: V. Q. Bao 61CSE103 Programming 1 45
Example 19
Instructor: V. Q. Bao 61CSE103 Programming 1 46
Example 20
Instructor: V. Q. Bao 61CSE103 Programming 1 47
Example 21
Instructor: V. Q. Bao 61CSE103 Programming 1 48
Example 22
Instructor: V. Q. Bao 61CSE103 Programming 1 49
Example 23
Instructor: V. Q. Bao 61CSE103 Programming 1 50
Example 23
Instructor: V. Q. Bao 61CSE103 Programming 1 51
7. Memory Functions of the String-Handling Library
Instructor: V. Q. Bao Programming 1 52
Instructor: V. Q. Bao 61CSE103 Programming 1 53
Example 24
Instructor: V. Q. Bao 61CSE103 Programming 1 54
Example 25
Instructor: V. Q. Bao 61CSE103 Programming 1 55
Example 26
Instructor: V. Q. Bao 61CSE103 Programming 1 56
Example 27
Instructor: V. Q. Bao 61CSE103 Programming 1 57
Example 28
Instructor: V. Q. Bao 61CSE103 Programming 1 58
Example 29
Instructor: V. Q. Bao 61CSE103 Programming 1 59
Example 30
Instructor: V. Q. Bao 61CSE103 Programming 1 60
8. Practical Exercises
1) Write a C program that add two or more complex numbers using
structure
2) Write a C program that read a book's information (tittle,
author, subject and book ID) and then print again
3) Write a C program that read a person's information (last
name, first name, age, nationality, height, weight, job tittle)
and then print again
4) Write a C program that add two distances (feet,inch) using
structure
Instructor: V. Q. Bao 61CSE103 Programming 1 61
8. Practical Exercises
5) Write a C program that ask user to choose 3 options, in
which:
- Option 1: convert a hour format from hh.mm.ss to second
- Option 2: convert second to a hour format hh:mm:ss
- Option 3: add two times (hh:mm:ss)
6) Create a structure Student with fields name, roll_number, and
marks. Write a program that allows the user to input the data
for 10 students, and create functions to:
Print all students who scored more than a specified grade.
Find the student with the highest marks.
Instructor: V. Q. Bao 61CSE103 Programming 1 62
THE END
Lecture 7: Working with Structure in C
INSTRUCTOR: Dr. Vuong Quoc Bao
Instructor: V. Q. Bao 61CSE103 Programming 1 63