1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
//---------------------------------------------
int main()
{
string s1, s2, s3;
//-------------------------------------------
cout << "Enter a sentence containing spaces, then press enter\n";
getline(cin, s1);
cout << "You entered [" << s1 << "]\n";
cout << endl;
//-------------------------------------------
cout << "Enter a sentence. Use the # char twice in the sentence, "
<< "then press enter.\n\n";
cout << "For example look at what follows and type it in exactly\n\n";
cout << "here is part#this is the second part # this is the end\n\n";
getline(cin, s1, '#');
getline(cin, s2, '#');
getline(cin, s3);
cout << endl;
cout << "First part is [" << s1 << "]\n\n";
cout << "Second part is [" << s2 << "]\n\n";
cout << "Third part is [" << s3 << "]\n\n";
cout << endl;
//-------------------------------------------
cout << "Enter a simple sentence and then press enter\n";
getline(cin, s1);
cout << "\nEnter a string to find in the sentence, it must exist: ";
getline(cin, s2);
size_t whereFound;
whereFound = s1.find(s2); // s2 does exist in s1
if (whereFound != string::npos)
cout << "That string starts at position " << whereFound
<< " in the sentence.\n\n";
cout << "Enter a string to find in the sentence, it must NOT exist: ";
getline(cin, s2);
whereFound = s1.find(s2); //s2 does not exist in s1
if (whereFound == string::npos)
cout << "That string does not exist in the sentence\n\n";
cout << "Value of whereFound = " << whereFound << "\n";
cout << "value of string::npos = " << string::npos << endl;
cout << endl;
//-------------------------------------------
s1 = "Here is a new sentence";
s2 = "is";
whereFound = s1.find(s2); //search s1 for the string in s2
if (whereFound != string::npos)
cout << "Found [" << s2 << "] at position "
<< whereFound << " in [" << s1 << "]\n\n";
whereFound = s1.find(s2, whereFound+1);
if (whereFound == string::npos)
cout << "[" << s2 << "] does not exist after that position\n\n";
s2="enterance";
whereFound = s1.find(s2.c_str(),0,2); // looking for "en" in s1
cout << "Found \"en\" at position " << whereFound
<< " in [" << s1 << "]\n";
whereFound = whereFound + 1;
whereFound = s1.find(s2.c_str(),whereFound,2); // looking for next "en" in s1
cout << "Found NEXT \"en\" at position " << whereFound
<< " in [" << s1 << "]\n";
cout << endl;
//-------------------------------------------
char lookForCh = 'e';
whereFound = s1.find(lookForCh,0); //first 'e'
whereFound = whereFound+1;
whereFound = s1.find(lookForCh,whereFound); //second 'e'
whereFound = whereFound+1;
whereFound = s1.find(lookForCh,whereFound); //third 'e'
cout << "3rd '" << lookForCh << "' found at position "
<< whereFound << " in [" << s1 << "]\n";
cout << endl;
return 0;
}
|