Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions