Chapter 6 (Part 3)
CS12 - Computer Programming 1
Chapter 6
1
An array whose components are of type char Recall: The most widely used character sets are ASCII and EBCDIC The first character in the ASCII character set is the null character, which is nonprintable the null character is represented as '\0', a backslash followed by a zero a string is a sequence of zero or more characters, and strings are enclosed in double quotation marks
Character Array and C-Strings are not exactly the same Chapter 6
CS12 - Computer Programming 1
2
C-strings are null terminated (the last character in a Cstring is always the null character)
A character array might not contain the null character, but the last character in a C-string is always the null character The following are examples of C-strings:
"John L. Johnson" "Hello there.“ „Hello‟ and “Hello” are different (the first
contains five characters only while the other one contains 6, having a null character) Chapter 6
CS12 - Computer Programming 1
3
Consider the following statement: char name[16]; C-strings are null termination and name has 16 components so the largest string that can be stored in name is of length 15 If you store a C-string of length 10 in name, the first 11 components of name are used and the last 5 are left unused The statement: char name[16] = {'J', 'o', 'h', 'n', '\0'};
is equivalent to char name[16] = "John"; Chapter 6
CS12 - Computer Programming 1
4
The statement: char name[] = "John"; take note that the size of the array is 5 Consider the followingstatement: char studentName[26]; Because aggregate operations, such as assignment and comparison, are not allowed on arrays, the following statement is not legal: studentName = "Lisa L. Johnson"; Chapter 6
CS12 - Computer Programming 1
5
C++ provides a set of functions that can be used for C-string manipulation described by string.h Three functions:
Chapter 6
CS12 - Computer Programming 1
6
strcat() – concatenates strings, puts one string at the end of another char First*40+ = “Peter”; char Second*40+ = “Pan”; printf(“%s”, strcat(First, Second)); strncmp() – compares only up to maximum characters specified. char First*40+ = “Peter”; char Second*40+ = “Pan”; if( strncmp(First, Second, 3) == 0) printf(“The first three characters are equal”); Chapter 6
CS12 - Computer Programming 1
7
toupper – a function that converts strings to uppercase Ex.: string[0] = toupper(string[0]); tolower – a a function that converts strings to lowercase Ex.: string[0] = tolower(string[0]);
Note: to use toupper and tolower for an entire array, use a for loop
Chapter 6
CS12 - Computer Programming 1
8
In C++, C-strings are compared character-bycharacter using the system’s collating sequence:
Examples: "Air" is less than "Boat" "Air" is less than "An" "Bill" is less than "Billy" "Hello" is less than "hello"
Chapter 6
CS12 - Computer Programming 1
9
Suppose you have the following statements: char studentName[21]; char myname[16]; char yourname[16];
Chapter 6
CS12 - Computer Programming 1
10
Given the declaration: char string15[16]; mark the following statements as valid or invalid. If a statement is invalid, explain why. a) strcpy(string15, "Hello there"); b) strlen(string15); c) string15 = "Jacksonville“; d) scanf(“%s”, &string15); e) printf(“%s”, string15); f) if (string15 >= "Nice day") printf(“%s”, string15); g) string15[6] = 't'; Chapter 6
CS12 - Computer Programming 1
11
A string variable could be used to store any ASCII characters Two ways of capturing value into string variable: scanf() function - stop conversion at the first whitespace or a maximum of 9 characters gets() function - does not limit the number of characters assigned to the string
Example: char product[9]; scanf(“%9s”, product);
Chapter 6
CS12 - Computer Programming 1
12