String Constructors string str; // creates an empty //string string str(“abc”); // creates a string // from a C-string string str(aString); // creates a string by // copying
String Methods str.substr(pos,len); // returns a substring starting a pos of // length len. str.length(); // returns the length os str. str.insert(pos,str2); // inserts str2 into str starting a pos. str.find(str1,pos); // finds str1 in str starting pos and returns // position, or npos if not found str.find(str1); // finds str1 in str and returns position, // npos is not found.
String Comparsions, Concat. str1 == str2 str1 != str2 str1 < str2 str1 <= str2... str1 + str2
Example - Sorting void swap(string& x, string& y) { string t = x; x = y; y = t; } int main(void) { string a[] = { "that", "cat", "rat", "bat", "sat"}; int size = 5; for(int j = 0; j < size; j++) for(int i = 0; i < size j; i++) if(a[i] > a[i+1]) swap(a[i], a[i+1]); for(int i = 0; i < size; i++) cout << a[i] << " "; cout << endl; }
Example – Text Processing Remember the rule: 'i' before 'e' except after 'c'? Suppose we want to write a program that tests if a word follows the rule. For example, “grief” and “receipt” are fine, but “caffeine” is not.
Solution #include using namespace std; int main(void) { string word; cout “; cin >> word; if(...) cout << “good” << endl;' else cout << “bad” << endl; }
Streams A stream is a flow of characters (or other data). If the flow is into the program, it is an input stream. If the flow is out of the program, it is an output stream. cin and cou t are two examples of streams. streams can also be related to files and can be used to read and write files. A third kind of stream is a pipe, where the input or output comes from or goes to another program.
Reading from a File The functions which deal with files are in the fstream library (for file stream). To read from a file, you must open the file for reading and associate an input stream with it. Reading from an input stream uses the extraction operator, >>, just like reading from cin.
Reading - Example #include using namespace std; int main(void) { ifstream myIStream; myIStream.open(“MyFile.txt”); string word; myIStream >> word;... myIStream.close(); } This opens the file “MyFile.txt” and associates the stream myIStream with the file. One string (word) is read in from the file.
Files have Two Names Note that there are two names associated with a file; the external name, associated with the operating system – in this case, “MyFile.txt” and and internal name for the stream, MyIStream. Once the file is opened, the program uses just the name of the stream.
Closing a File Closing a file disconnects the stream from the file, and performs some clean-up operations, so don't forget to close a file once you're done with it.
Writing to a File The functions which deal with files are in the fstream library (for file stream). To write to a file, you must open the file for writing and associate an output stream with it. Writing to an output stream uses the insertion operator, <<, just like writing to cout.
Writing - Example #include using namespace std; int main(void) { ofstream myOStream; myOStream.open(“MyOutput.txt”); string word = “foobear”; myOStream << word;... myOStream.close(); } This opens the file “MyOutput.txt” and associates the stream myOStream with the file. One string (word) is written to the file.
Appending to a File You can append text to a file, by using the code: ofstream myOStream; myOStream.open(“MyOutput.txt”, ios::app); Don't forget to close the file when done writing to it.
Checking for the end of file The method eof() is a Boolean method that will check if the program is at the end of the file, e.g., myIStream.eof(). Beware – it only returns true once you've tried to read past the end of the file (and not when you're about to read past the end of the file). Using (myIStream >> word) as your test is probably a better practice.