Question: Prompt the user for a string, read in a single word. Now, using the find function, report the index of the first occurrence of: 1)
Prompt the user for a string, read in a single word.
Now, using the find function, report the index of the first occurrence of:
1) the character . and
2) the location of the first occurrence of the string stop.
If what you are searching for is NOT present in the string, report that fact.
Remember, the find function can take an argument of type char, or of type string, and if the requested value is not present, the function returns the special value string::npos
Input from the keyboard is shown bolded and underlined
| Example 1: Which exercise? 3 Enter a sentence: home?? Your sentence does not contain the character '.' Your sentence does not contain the word "stop" |
| Example 2: Which exercise? 3 Enter a sentence: pit-stop. The character '.' is located at index 8 The word "stop" starts at index 4 |
I have started the code but am not getting the appropriate response and I am not sure how to proceed with it...
#include
using namespace std;
int main()
//variables
string sentence;
cout << "Enter a sentence: "; cin >> sentence;
for ( int count = 0; count < sentence.size(); count++)
{ if ((sentence.find("stop")) && (sentence.find('.'))) { cout << "The character '.' is located at index " << sentence.find('.') << endl; cout << "The word \"stop\" is located at index " << sentence.find("stop") << endl; }
else if (sentence.find("stop")) { cout << "The word \"stop\" is located at index " << sentence.find("stop") << endl; }
else if (sentence.find('.')) { cout << "The character '.' is located at index " << sentence.find('.') << endl; }
else if ((sentence.find("stop")) && (!(sentence.find('.')))) { cout << "Your sentence does not contain the character '.'" << endl;
cout << "Your sentence does not contain the word \"stop\" " << endl;
}
else if ((!(sentence.find("stop"))) && (sentence.find('.'))) { cout << "The character '.' is located at index " << sentence.find('.') << endl; cout << "The word \"stop\" is located at index " << sentence.find("stop") << endl; }
}
}
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
