To store word and sentences, you can use the string class. String is not a fundamental C++ variable type, but a class you can create objects from. The data is actually an array of characters (char). A string can store things such as "Hello" or "I am 21 today!". Strings are enclosed in quotations (double quotes) – unlike char variables which are enclosed in apostrophy's (single quotes).
The program below shows how to declare a variable of type string, assign it a value (Hello World!), and print it out.
Program 15.1a – Declaring and Using a String |
#include <iostream> |
Output |
The next program shows how strings can be concatenated (joined together) using the plus sign. Notice how a space between the two words was included by placing it in the double quotes " ".
Program 15.1b - Concatenating Strings |
#include <iostream> |
Output |
Strings can be compared. The next program compares "apple" to "apricot" to see which one is greater. When comparing strings, the ASCII number for each letter is compared (refer to the ASCII chart). Try running the same program with "Apricot" capitalized.
Program 15.1c - Comparing Strings |
#include <iostream> |
Output |
The cin statement will truncate a string at the first space as shown in the program below.
Program 15.2a - String Truncation |
#include <iostream> |
Output |
Program 15.2b - No String Truncation |
#include <iostream> |
Output |
When you declare a string, you are creating an object of the string class (instead of declaring a primitive variable). Since string is a class, there are many functions you can use.
Assume: string A = "have a nice day";
Function | Returns | Description |
A.size() | 15 | Returns the number of characters in A (including spaces) |
A.substr(7,4) | "nice" | At position 7, returns 4 characters |
A.find("n",0) | 7 | Returns the location of the first "n" beginning at postion 0. If it is not found, it returns -1. |
A.find("a",7) | 13 | Returns the location of the first "a" beginning at postion 7. |
A.insert(7,"very ") | "have a very nice day" | At position 7, insert "very ". Important: The insert function changes the value of A. |
A.replace(7,4,"swell") | have a swell day | At position 7, replace the 4 characters ("nice") with "swell". Important: The replace function changes the value of A. |
The programs below demonstrate the string functions.
Program 15.3a - Substring Function |
#include <iostream> |
Output |
Program 15.3b - Insert Function |
#include <iostream> |
Output |
Program 15.3c - Replace Function |
#include <iostream> |
Output |
Program 15.3d – Search and Replace |
#include <iostream> |
Output |
What will the following cout statements print?
#include <iostream.h>
int main() cout << B[0] << B[1] << B[2]; cout << A.substr(0,4) << B.substr(9,4); cout << A.substr(5,3) << B[6]; A.replace(5,3,"eve"); cout << A; cout << A.find("e",0); cout << B.substr(0,5); A.insert(0,"very "); cout << A;
return 0; |
5. (a) goo (b) mor (c) alp 6. (a) goodsoup (b) good soup 7. (a) mornbe (b) more (c) mor
8. (a) good evening (b) good eve 9. (a) e (b) 5 (c) v (d) 7 10. (a) alpha (b) good (c) bet
11. (a) very alpha |