// This program demonstrates the member functions: // find // replace // erase // insert // substr /* LAB2: Initialize a string object to be some long text. Read in a pattern from the user. Search for the pattern in the text. Print the location if found, otherwise print NOT FOUND. Loop until the user enters "done" as the pattern. */ #include #include using namespace std; int main() { string text="Find a small needle inside a large haystack..."; string pattern="needle"; string pattern2="needles"; string::size_type index; index = text.length(); cout << "length of text is: " << index << endl; // the object before the DOT is the text that you are searching // the object that is the parameter is what you are looking for index = text.find(pattern, 0); if (index == string::npos) // if not found cout << "Your pattern " << pattern << " is not found."; else cout << pattern << " found at position: " << index << endl; index = text.find(pattern2, 0); if (index == string::npos) // if not found cout << "Your pattern " << pattern2 << " is not found."; else cout << "found at position: " << index << endl; return 0; }