.Open The C++ Standard string library C++ has a powerful and useful class of objects in its library. They are called `strings`. They contain characters. You can use them to model words and character data. They can contain any characters or no characters. They can change size as you need. You can input them and output them. You can also "slice and dice" them. The characters in a string are numbered, starting at 0 -- don't ask why! So `string("12345")` has the character "1" in place 0, "2" in place 1, and so on. These notes give a simplified description of the ways they can be used. . Include this library The easy way to use strings is to put these lines at the start of your program .As_is #include .As_is using namespace std; . Declaring a string variable .As_is string newVariable = "initial value"; .As_is string newVariable = anyStringExpression; . Constructing a C++ string from a litteral .As_is string ("example"); This is also use to construct a C++ string from an old C char*: .As_is char * cp = "example"; .As_is string (cp); . Accessing data about a string == length and empty If you have a string called `s` then .As_is s.length() is the number of characters in the string, and .As_is s.empty() is true if the string is empty. So, for example, .As_is string("example").length() has value 7. . Accessing the old-style C char* in a string This expression gives you the array of chars, terminated by a null '\0' char: .As_is s.c_str() . Concatenating two or more strings You can use the "+" symbol to combine strings. For example, if we declare .As_is string a = "abra"; .As_is string b = "ca"; .As_is string c = "dabra"; then .As_is cout << a + b + c < s2 .As_is s1<=s2 .As_is s1>=s2 Again one of the strings must be modern C++ string. `"bad" < "cad"` does not compare the contents of the literals. . Input words into a string This stops at the first whitespace character but does not store it. .As_is cin >> word; . Input lines into a string Use .Key getline like this .As_is getline (cin, line); This works with all input streams. . Output strings .As_is cout << stringexpression ... . Working on a character in a string If `s` is a string and `i` is an positive integer that is less than `s`.lenght() then .As_is s[i] is a character in the string and .As_is s.substr(i,1) is a string containing the `i`'th character in `s`. Note: the characters are numbered 0,1,2,3,..... For example, .As_is string example="example"; means .As_is example[1] is 'x' not 'e'. Similarly `example[6]` is an 'e' and the last valid character. Warning: bad things happen if you go outsude the range 0,1,2,.... ,length()-1. . Single characters are typed with single quotes For historical reasons, 'x' is a character and "x" is a string of two characters! . Working on each character in a string in turn Use a .Key for loop. Here is an example, that replaces each 'a' in a string `s` by `1`: .As_is for(int i = 0; i < s.length(); i++) .As_is { .As_is if( s[i] == 'a' ) .As_is s[i] = '1'; .As_is } Here is a way to use a for loop to output a string backwards .As_is for(int i = 0; i < s.length(); i++) .As_is { .As_is cout << s[s.length()-i-1]; .As_is } Exercise: try running this fragment by hand with string("abcd"). I tested the code in .See ../cs201/back.cpp Exercis: can you right the for loop that starts at the end of the string and works back to 0 (and no further)? . Inputting one character at a time You can read data from input one character at a time and put it in a string if you want. Here is a sample .See ../cs201/backwards.cpp of using `cin.get(c)`. . How can I convert numbers to strings and strings to numbers I've dug up a couple of advanced types of objects "$stringstreams" that do the job if you need it in a project, for example. Here .See ../cs201/stringnumbers.cpp is a program that demonstrates how to use a couple of functions "convertDouble" and "convertToDouble" that are defined in .See ../cs201/stringnumbers.h that you can download and #include in any of your programs. .As_is string convertDouble(double d) .As_is { .As_is std::ostringstream s; .As_is s << d; .As_is return s.str(); .As_is } .As_is double convertToDouble(string d) .As_is { .As_is double result; .As_is std::istringstream s(d); .As_is s >> result; .As_is return result; .As_is } . stringstreams Stringstreams are a very powerful feature. Suppose you have a line that describes a person. It has the first name, their second name, there phone number, and their age. Each is a field with no spaces. Between them is one or more spaces: .As_is John Doe 909-883-1234 31 .As_is Jane Row 714-123-4567 21 Then we can write: .As_is string line; .As_is string first, second, phone; int age; .As_is getline(input, line); .As_is std::istringstream data(line); .As_is data >> first >> second >> phone >> age; to unpack the data. . KISS file handling Here is an example of the kind of simple code you can use that combines stringstreams, objects, etc. .See ../cs201/Widget.h .See ../cs201/listWidgets.cpp .Close The C++ Standard string library