To OO workshop particpants Comments: from Rick Mercer 5-Oct-1995 I believe each class (string, vector, stack, iterators,...) was to to have three files: string.txt This TUTORIAL string.h class declaration for documentation (and compilation?) string-d.cpp An executable program with emonstrating all functions with comments and assertions to explain code. I can't remember who wrote the following tutorial, but Joel Adams talked about it in the wrapup. It is unedited except for one correction concerning string input and I indented code. I take no responsibility for it's content (I do with the other two files however). IMPORTANT: No attempt has been made to match this file with the other two. I am waiting to confirm exactly what we are going to do. Since much of this information is repeated in string-d.cpp, so I favor combining this material with the other. All code would then exist within the context of a program and use comments to explain. What do you think? /*------------------TUTORIAL----------------- String Readme: Level 0: String README: (Part I) A string is an ordered sequence of characters. String objects are declared and intitialized with statements like this string str; constructs an instance of string called str with length 0 (empty string). Notice that the function empty returns true if length is 0. Thus for our string str, we would have str.empty() returns true. For a nonempty string it returns false. (The type returned is called bool for Boolean.) You can assign strings. For example, we could assign any literal. For example: str = "Hi, there"; would put the string Hi, there into str. The length of str would be 9 and you can get that length by calling the member function length. Thus, if you wrote cout << str.length(); the value 9 would be printed. You can also do input. For example cin >> str; where you type in Hi, there at the keyboard would put Hi, into str. To capture an entire line of string data, use the getline function: getline(cout, str); You can print out str with the statement cout << str; which simply copies str to standard output. One could also do an initialized declaration as one statment string str2 = "Goodbye"; which would put Goodbye into str2. You can assign one string variable with another. Thus, if we write str = str2;, both would contain Goodbye. --------------------------------------------------------------------------- --------------------------------------------------------------------------- String Readme Part II: (Later in the course). We can add one string to another with the + operator. This addition of strings is called concatenation. For example, string s1 = "Hello, "; string s2 = "World!"; string s3; s3 = s1 + s2; Causes s3 to contain Hello, World! We can also use literals. For example: string s4 = "Goodbye, " + s2; would cause s4 to contain Goodbye, World! or string s5 = s1 + "Galaxy!"; to get s5 to contain Hello, Galaxy! We can initialize a string with a character. For example string str4 = ch; which makes str4 a string with just the single character ch. After such assignments, the length is always 1. We can access individual characters within a string directly using the [] operation. Thus, string str3 = "Hello, World!"; char ch = str3[0]; would put character H into variable ch. (The first character in a string is always at position 0 in the string, the second at 1, ..., the last at position length() -1. We can also copy characters inside the string. For example, in our string str3 we could copy the l in position 3 to replace the d in position 11 by str3[11] = str3[3]; This would convert the contents of str3 to Hello, Worll! You can assign a character to a position. For example, str3[1] = ch; An attempt to access an illegal position will cause an error. For example, since our string str3 has only 12 characters in positions 0-11, a use of str3[15] is a logical error. Any attempt to access at positions less than 0 or greater than or equal to length is a logical error and is undefined. We could also assign a character to a string. For example: char ch = 'D'; str = ch; causing str to be a string with just the character D in it. One can get a substring by calling the member function substr with the starting position and the size of the string. Thus, string s5 = "Hello, World!"; s5.substr(2,3) would return the string llo. The following code deletes the substring of str beginning at pos of length n. str.remove(pos, n); The following code returns the position where str1 starts in string str2. find(str1, str2) The following code puts string str1 into string str2 starting at position pos. insert(str1, str2, pos) The usual relational operators, ==, !=, <, >, <=. >= all work as expected. That is, in normal ASCII collating order which is like dictionary order except upper case precede lower case and blanks precede both. Set diagram: Basic conceptual ideas are Construction, length, empty. Then you add output, input, assignment. Then add concatenate. Then add Intra-string operations: indexing, substring, find, remove, insert. Then add relationals.