Lecture 7
Strings
Strings are a concatenation of characters enclosed in quotation marks. String is a data type that is not a part of C++ but is programmer defined from the use of including the standard C++ library header file string.
Review string operations
string name;
ifstream indata;
indata.open(“data.txt”);
cin >> name; // reads in characters from keyboard until white space one
indata>> name;// reads in characters from data file, data.txt, until white space one
getline ( cin, name); // reads in from keyboard all characters of name including white space ones
getline (indata, name); // reads in from data file all characters of name including white space ones
relational operators between string variables continue until there is a mismatch. If none is found they are the same (==). If the first difference between the series of characters is found the character lower in the ascii numbering value is considered the smaller string
str1 = “the”;
str2 = “tag”;
(str2 < str1) evaluates to true
New data types
The string header file introduces some useful new data types
string::size_type ; // an unsigned integer data type
string:npos; // the maximum value of the string::size_type
New operations
Can add.strings using the + operator
e.g. str1=”Bob”;
str2=”Jones”
str3=stri + str2;
then str3=BobJones
well what if we want a space between the first and last name
solution is to have str3=str1 + “ “ + str2;
Warning For the operator + to work with string one of the operands must be a string variable. For example str3 = “Bob “ + “Jones” is illegal
Array subscript operator [] causes string replacement of a character at a string position
String str1 = “hello There”;
Str1[0]=’H’; // this replaces the first character position with ‘H’
Length function
string_Variable.length(); // returns the number characters in a string as an unsigned integer. The value can then be returned to a variable. Since string::size_type is associated with string the variable used for the returning value should be of this type.
e.g. string str1;
str1 = “Bob Jones”;
string::size_type len;
len = str1.length(); // value of len = ?
string_Variable.size(); // Does the same as the function length
Find function
string_Variable.find(string_expression); //searches a string for a substring and returns the position of the occurrence of the first character of the matching substring. If not successful returns the value of string::npos (not a position within the string). The position value can be returned to a variable of string::size_type.
String_Variable.find(string_expression, position); // As above function but starts searching at the position given in the parameter. Positions always begin with 0.
e.g. string str1;
str1 = “Bob Jones”;
string::size_type position;
position = str1.find(ob); // value of position = ?
Substring function
string_Variable.substr(position, length); //returns a substring of the length specified in the function parameter starting at the position indicated.
e.g. string str1, str2;
str1 = “Bob Jones”;
str2 = str1.substr(0, 5); // str2 = ?
Swap Function
string_Variable1.swap(string_Variable2); //After execution of this statement both string variables exchange their contents
Exercise
string message = “Hello world”;
cout << message.length() << endl;
string message = “Hello world”;
cout << message[3] << endl;
string message = “Hello world”;
cout << message.substr(6,5);
string message = “Hello world”;
cout << size() <<endl;
header file.