Topic 11 Strings Objectives In this chapter you

Topic 11: Strings

Objectives: In this chapter, you will learn about: • characters and strings • convert strings to different cases • discuss and use ASCII Table • use built-in functions from Standard Template Library (STL) • Common programming errors An Introduction to Programming with C++, Seventh Edition 2

The string Data Type • The string data type is not a fundamental data type in C++ • Added to C++ through use of string class • To use the string class, a program must contain the #include <string> directive • You can use the string class to create string variables or string named constants • Memory locations of type string are initialized with string literal constants— 0 or more characters enclosed in double quotation marks (“”) An Introduction to Programming with C++, Seventh Edition 3

The string Data Type (cont’d. ) Figure 13 -1 How to declare and initialize string variables and named constants An Introduction to Programming with C++, Seventh Edition 4

The string Data Type (cont’d. ) An Introduction to Programming with C++, Seventh Edition 5

The getline Function • The getline function obtains string data from the keyboard and stores it in a string variable • Syntax is: getline(cin, string. Variable. Name [, delimiter. Character]); • Three actual arguments (first two required): – cin argument refers to computer keyboard – string. Variable. Name argument is name of a string variable in which to store input – Optional delimiter. Character indicates character that immediately follows last character of string An Introduction to Programming with C++, Seventh Edition 6

The getline Function (cont’d. ) • Function will continue to read characters entered at keyboard until it encounters a delimiter character • Default delimiter character is newline character • When the function encounters a delimiter character, it discards the character—process called consuming the character • Newline character is designated by ‘n’ • Backslash is called an escape character • Backslash and character following it are called an escape sequence An Introduction to Programming with C++, Seventh Edition 7

The getline Function (cont’d. ) Figure 13 -4 How to use the getline function to get string input from the keyboard An Introduction to Programming with C++, Seventh Edition 8

The getline Function (cont’d. ) Figure 13 -4 How to use the getline function to get string input from the keyboard (cont. ) An Introduction to Programming with C++, Seventh Edition 9

The getline Function (cont’d. ) An Introduction to Programming with C++, Seventh Edition 10

The getline Function (cont’d. ) An Introduction to Programming with C++, Seventh Edition 11

The getline Function (cont’d. ) An Introduction to Programming with C++, Seventh Edition 12

The getline Function (cont’d. ) An Introduction to Programming with C++, Seventh Edition 13

The ignore Function • The ignore function instructs the computer to read and ignore characters stored in the cin object by consuming (discarding) them • Syntax is: – cin. ignore([number. Of. Characters][, delimiter. Character]); • Has two actual arguments, both optional: – number. Of. Characters argument is maximum number of characters function should consume (default is 1) – delimiter. Character argument stops ignore function from reading and discarding any more characters when consumed An Introduction to Programming with C++, Seventh Edition 14

The ignore Function (cont’d. ) Figure 13 -9 How to use the ignore function An Introduction to Programming with C++, Seventh Edition 15

The ignore Function (cont’d. ) An Introduction to Programming with C++, Seventh Edition 16

The ZIP Code Program • Program reads in a string from the user and verifies that it contains exactly five characters • If string contains exactly five characters, program displays “Valid length” • Otherwise, displays “Invalid length” An Introduction to Programming with C++, Seventh Edition 17

Determining the Number of Characters in a string Variable • You use string class’s length function to determine the number of characters in a string variable • Syntax is: – string. length() • Returns number of characters contained in string An Introduction to Programming with C++, Seventh Edition 18

Determining the Number of Characters Contained in a string Variable (cont’d. ) Figure 13 -13 How to use the length function (cont’d. ) An Introduction to Programming with C++, Seventh Edition 19

Determining the Number of Characters Contained in a string Variable (cont’d. ) Figure 13 -14 The ZIP code program An Introduction to Programming with C++, Seventh Edition 20

Determining the Number of Characters Contained in a string Variable (cont’d. ) Figure 13 -15 Sample run of the ZIP code program An Introduction to Programming with C++, Seventh Edition 21

Concatenating Strings • String concatenation refers to the process of connecting (linking) strings together • You concatenate strings using the concatenation operator (+ sign) An Introduction to Programming with C++, Seventh Edition 22

Concatenating Strings (cont’d. ) Figure 13 -41 How to use the concatenation operator An Introduction to Programming with C++, Seventh Edition 23

Some String Library Functions • C++ supports a wide range of string manipulation functions. The most common are – – strcpy( ) strcat( ) strcmp( ) strlen( ) • The string functions all use the same header, <cstring>. Let’s take a look at these functions now. An Introduction to Programming with C++, Seventh Edition 24

strcpy() • A call to strcpy( ) takes this general form: strcpy(to, from); • The strcpy( ) function copies the contents of the string from into to. – Remember, the array that forms to must be large enough to hold the string contained in from. If it isn’t, the to array will be overrun, which will probably crash your program. An Introduction to Programming with C++, Seventh Edition 25

strcat() • A call to strcat( ) takes this form: strcat(s 1, s 2); • The strcat( ) function appends s 2 to the end of s 1; s 2 is unchanged. You must ensure that s 1 is large enough to hold its original contents and those of s 2. An Introduction to Programming with C++, Seventh Edition 26

strcmp() • call to strcmp( ) takes this general form: strcmp(s 1, s 2); • The strcmp( ) function compares two strings and returns 0 if they are equal. – If s 1 is greater than s 2 lexicographically (that is, according to dictionary order), then a positive number is returned; if it is less than s 2, a negative number is returned. • The key to using strcmp( ) is to remember that it returns false when the strings match. An Introduction to Programming with C++, Seventh Edition 27

strlen() • The general form of a call to strlen( ) is strlen(s); • where s is a string. The strlen( ) function returns the length of the string pointed to by s. An Introduction to Programming with C++, Seventh Edition 28

A String Function Example • The following program illustrates the use of all four string functions: Here is the output: lengths: 3 22 not equal C++ is power programming. and C++ is power programming. s 1 and s 2 are now the same An Introduction to Programming with C++, Seventh Edition 29

Common Programming Errors • Forgetting to include string header file when using string class object An Introduction to Programming with C++, Seventh Edition 30
- Slides: 30