Presentation is loading. Please wait.

Presentation is loading. Please wait.

String Constructors string str; // creates an empty //string string str(“abc”); // creates a string // from a C-string string str(aString); // creates.

Similar presentations


Presentation on theme: "String Constructors string str; // creates an empty //string string str(“abc”); // creates a string // from a C-string string str(aString); // creates."— Presentation transcript:

1 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

2 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.

3 String Comparsions, Concat. str1 == str2 str1 != str2 str1 < str2 str1 <= str2... str1 + str2

4 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 - 1 - 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; }

5 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.

6 Solution #include using namespace std; int main(void) { string word; cout “; cin >> word; if(...) cout << “good” << endl;' else cout << “bad” << endl; }

7 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.

8 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.

9 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.

10 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.

11 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.

12 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.

13 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.

14 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.

15 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.


Download ppt "String Constructors string str; // creates an empty //string string str(“abc”); // creates a string // from a C-string string str(aString); // creates."

Similar presentations


Ads by Google