20
C Strings

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:

Embed Size (px)

Citation preview

Page 1: 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:

C Strings

Page 2: 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:

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;

Page 3: 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:

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.

Page 4: 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:

Binary Numbers Any pattern of 0’s and 1’s

represent a numeric value. Examples:0 0 0 0 0 0 0 1

0 0 0 0 0 0 1 0

0 0 0 0 0 0 1 1

Page 5: 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:

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. (27 = 128.)

An ASCII code chart will thus have 128 entries; because we start numbering from zero, the entries will be numbered 0 through 127.

Page 6: 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:

ASCII Chart ASCII Code Chart

Page 7: 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:

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.

Page 8: 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:

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

Page 9: 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:

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:

Page 10: 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:

Character arrays are used to store C strings. For example:

char lastName[10] = "Jackson";

will cause the array, lastName to contain:

Page 11: 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:

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:

Page 12: 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:

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';

Page 13: 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:

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:

Page 14: 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:

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.

Page 15: 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:

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.

Page 16: 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:

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.

Page 17: 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:

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)

Page 18: 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:

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.

Page 19: 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:

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

contain:

Page 20: 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:

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.