Title: COMP102 Session 3A
1COMP102 Session 3A
- Lab10 Guessing the Capitals
2Character Strings
- A sequence of characters
- Referred to as a character string
- Stored in an array of type char
- Ending
- With the null character
- '\0'
3Character Strings
- A character string is a sequence of characters
enclosed in double quotes - E.g.
- char s12 a
- char message12 Hello world
4getline
- Member function of cin
- Get a character string from input
- E.g.
char lastName30, firstName30 cin.getline(la
stName, sizeof(lastName), ',') cin.getline(firstN
ame, sizeof(firstName))
5String Copy Function
- Include ltstring.hgt
- Copies source string into destination string
- void strcpy(char dest, const char src)
- E.g.
- char str16
- strcpy(str, Hello World)
6String Length Check Function
- Include ltstring.hgt
- Returns length of string
- int strlen(const char)
- Not including \0
- E.g.
- int string_length strlen("abcde")
- strncpy(str_1, str_2, strlen(str_1))
7String Comparison
- Compares strings s1 and s2
- int strcmp(char s1, char s2)
- if (s1 lt s2)
- Return -1
- if (s1 s2)
- Return 0 (false)
- if (s1 gt s2)
- Return 1
- int strncmp(char s1, char s2, int limit)
- Same as strcmp
- Except that at most limit characters are compared
8Common Errors
- Illegal to use to assign value to a string
variable - Except at declaration
- char str6 Hello
- E.g.
- str Hello
- Use strcpy function instead
- strcpy(str, Hello)
9Common Errors
- The operator doesn't test two strings for
equality - E.g.
- if (string1 string2)
- Use strcmp function instead
- if (!strcmp(string1, string2))
10Structure
- Structure User defined data types
- Collection of related data items
- Possibly of different types
- In C
- Called struct
- Can be composed of data of different types
- Array
- Can contain only data of the same type
11Structure
- E.g.
- Student record
- Student id, name, major, gender, start year,
- Bank account
- Account number, name, currency, balance,
- Address book
- Name, address, telephone number,
12Structure
- Individual components of a struct type
- Called members (or fields)
- Members can be of different types
- int, float, double, char, array, or even another
struct - A struct is named as a whole
- While individual members are named using field
identifiers - Complex data structures
- Can be formed by defining arrays of structs
13struct Basics
- Definition of a structure
- E.g.
struct ltstruct-typegt lttypegt
ltidentifier_listgt lttypegt ltidentifier_listgt
struct date int day int month int
year
struct studentRecord char gender int
id char dept5 char name20
14struct Basics
- Declaration of a variable of struct type
- Syntax
- ltstruct-typegt ltidentifier_listgt
- E.g.
- studentRecord student1, student2
- student1 and student2 are variables of
studentRecord type
15struct Basics
- The members of a struct type variable are
accessed with the dot (.) operator - Syntax
- ltstruct-variablegt.ltmember_namegt
- E.g.
- student1.gender 'M'
- student1.id 12345
- strcpy(student1.dept, "COMP")
- strcpy(student1.name, "Chan Tai Man")
16struct Basics
17struct-to-struct Assignment
- The values contained in one struct type variable
- Can be assigned to another variable
- Of the same struct type
- E.g. student1 student2
- E.g.
18Nested Structures
- Nest structures inside structures
- E.g.
struct point double x, ypoint
Pstruct line point p1, p2line
Lstruct triangle point p1, p2,
p3triangle T
19Arrays of Structures
- An array of structs
- Multiple types of data in each array element
- E.g.
studentRecord class100class98.gender
'M'class98.id 12345strcpy(class98.dept,
"COMP")strcpy(class98.name, "Chan Tai
Man")class0 class98
20Arrays inside structures
- We can use arrays inside structures
- E.g.
- Assign values to sq using the given square
struct square point vertex4 square
sq
21Initialize Data of struct Type
- Using assignment during declaration
- E.g.
// student record struct StudentRecord
double totalgrade char namename_size
char idid_size int gradeno_grades
StudentRecord student1 0.0, "CHAN Tai
Man", "123456", 80, 67, 34, 67
22Enumerated Type
- The enumerated type (enum)
- Derived from the integer type
- Each integer value is given an identifier
- Called an enumeration constant
- Syntax
- enum type_name enumeration_constants
- type_name variable_name
23Enumerated Type
enum days mon, tue, wed, thur, fri, sat,
sun days day_of_week // normal expression
day_of_week wed // ERROR miss match type
day_of_week 0 // CORRECT 0 is converted to
days day_of_week (days) 0 // Use
enumeration constant as an integer int x
tue // same as x 3 int day7
daytue 0 // same as day1 0
24enum and struct
enum suit club, diamond, heart, spade struct
card suit cardSuit int value