Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:

Similar presentations


Presentation on theme: "C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:"— Presentation transcript:

1 C Strings

2 The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples: const char ASTERISK = '*'; char ch; char letter; letter = 'a'; cin >> ch; cout << letter;

3 How are characters stored? The char data type sets aside one byte to store a character value. Recall that one byte equals 8 bits, where each bit contains either a zero or a one. What values do the 8 bits have when they represent various characters? A common encoding scheme for character data is ASCII, which stands for American Standard Code for Information Interchange.

4 Binary Numbers Any pattern of 0’s and 1’s represent a numeric value. Examples: 0 0000001 00000010 00000011

5 ASCII code In the ASCII code, bit 7 is always a zero. Since this leaves us with 7 bits 6 through 0 for our code, this means there are 128 possible patterns. (2 7 = 128.) An ASCII code chart will thus have 128 entries; because we start numbering from zero, the entries will be numbered 0 through 127.

6 ASCII Chart ASCII Code Chart

7 C-style Character Strings (C strings) Recall that the language C++ was based on the earlier language C. Unlike C++, the C language does not have the string data type. Instead, in C, strings of characters are stored in one-dimensional arrays of type char, one character per array element.

8 When we have placed a string of characters in double quotes, say in an output instruction, we have used C strings. For example: cout << "x="; Thus a list of characters contained in double quotes is a C-style character string, or C string. Because C strings are stored as an array, the C string "x=" is stored as

9 A C string is terminated by the special character '\0' called the NULL character. The NULL character acts as a sentinel and marks the end of a C-style character string. The string “x” and the character ‘x’ are stored differently as the pictures below demonstrate:

10 Character arrays are used to store C strings. For example: char lastName[10] = "Jackson"; will cause the array, lastName to contain:

11 If instead, we had initialized lastName with a string bigger than can be stored in the array, for example: char lastName[10] = "Washington"; then no error message will be generated in C++, but the following unfortunate situation will occur:

12 Input of C Strings char line[80]; char ch; int charCount = 0; // Read a line of text into the character // array "line". If the line is too long, // only read the first 79 characters cin.get(ch); while ( ch!='\n' && charCount<79 ) { line[charCount] = ch; charCount++; cin.get(ch); } // Place the NULL terminating character in the last position line[charCount] = '\0';

13 The operator >> can be used to read in a C string char name[20]; cout << "Please enter the name: " cin >> name; and our input is: Mouse, Mickey then internally, name will contain:

14 Output of C Strings the C string can be printed all at once using cout. Assume name is a character string initialized as follows: char name[15] = "Washington"; The output cout << endl << name; will cause the string "Washington" to be printed in the leftmost 10 positions of a new line.

15 Comparison of C Strings C++ provides an extensive collection of functions that allow the programmer to manipulate C strings. To use these functions, the string.h header file must be included in your program.

16 The function strcmp() has been provided to compare two C strings, say str1 and str2. If str1 < str2 (based on a character by character comparison), a value less than 0 is returned. If str1 and str2 are the same, a value of 0 is returned. If str1 > str2, a value greater than 0 is returned. The function makes character comparisons of the elements in str1 and str2 starting with the 0th character. It stops when it finds characters that are not equal or when it reaches the end of one of the strings.

17 Examples of String Comparisons: strcmp("A","B") returns < 0 (negative) strcmp("James","Jami") returns < 0 (negative) because 'e' < 'i' strcmp("135", "24") returns < 0 (negative) because '1' < '2' strcmp("ABCD","ABC") returns > 0 (positive) strcmp("ABC","ABCD") returns < 0 (negative) strcmp("89", "89") returns 0 (zero)

18 Copying (assigning) a C String C string assignment (or string copy) is achieved by the function strcpy(). This function has two arguments, a destination string and a source string. No check is made to determine whether or not the destination string has enough space! All characters from thesource up to and including the '\0' are copied.

19 Example of Copying a string: Example: char name[15]; strcpy(name, "Mr. Mouse"); will cause the variable name to contain:

20 C String Length C++ provides a function,called strlen(), that returns the number of characters in a C string (up to but not including the NULL character). For example, strlen("Mr. Mouse") returns the value 9.


Download ppt "C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:"

Similar presentations


Ads by Google