C Programming From Problem Analysis to Program Design

C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 8: The string Type

The string Type • To use the data type string, the program must include the header file <string> • The statement: string name = "William Jacob"; declares name to be a string variable and also initializes name to "William Jacob" • The first character, 'W', in name is in position 0; the second character, 'i', is in position 1, and so on

The string Type (continued) • The variable name is capable of storing any size string • Binary operator + (to allow the string concatenation operation), and the array subscript operator [], have been defined for the data type string • For example, If str 1 = "Sunny", the statement stores the string "Sunny Day" into str 2: str 2 = str 1 + " Day";

Input/Output and the string Type • The function getline − Reads until end of the current line − string s; − getline(cin , s); − cout<<s<<endl; //accept spaces

length Function • Length returns the number of characters currently in the string • The syntax to call the length function is: str. Var. length() where str. Var is variable of the type string • length has no arguments • length returns an unsigned integer • The value returned can be stored in an integer variable

size Function • The function size is same as the function length • Both functions return the same value • The syntax to call the function size is: str. Var. size() where str. Var is variable of the type string • As in the case of the function length, the function size has no arguments

Example 8 -14: clear, empty, erase, length, AND size FUNCTIONS The length function return a value of size_type data type. 7

find Function • find searches a string for the first occurrence of a particular substring • Returns an unsigned integer value of type string: : size_type giving the result of the search • The syntax to call the function find is: str. Var. find(str. Exp) where str. Var is a string variable and str. Exp is a string expression evaluating to a string • The string expression, str. Exp, can also be a character

find Function (continued) • If successful, find returns the position in str. Var where the match begins • For the search to be successful the match must be exact • If unsuccessful, find returns the special value string: : npos (“not a position within the string”)


substr Function • substr returns a particular substring of a string • The syntax to call the function substr is: str. Var. substr(expr 1, expr 2) where expr 1 and expr 2 are expressions evaluating to unsigned integers

substr Function (continued) • The expression expr 1 specifies a position within the string (starting position of the substring) • The expression expr 2 specifies the length of the substring to be returned


swap Function • swap interchanges the contents of two string variables • The syntax to use the function swap is str. Var 1. swap(str. Var 2); where str. Var 1 and str. Var 2 are string variables • Suppose you have the following statements: string str 1 = "Warm"; string str 2 = "Cold"; • After str 1. swap(str 2); executes, the value of str 1 is "Cold" and the value of str 2 is "War"

More Functions 15

More Functions 16

More Functions 17

Reading from files
- Slides: 18