TheAlgorithms-C-Plus-Plus/search/searching.cpp

41 lines
1.1 KiB
C++
Raw Normal View History

2017-12-24 01:30:49 +08:00
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
char paragraph;
int main()
{
2019-08-21 10:10:08 +08:00
string paragraph;
cout << "Please enter your paragraph: \n";
getline(cin, paragraph);
cout << "\nHello, your paragraph is:\n " << paragraph << "!\n";
cout << "\nThe size of your paragraph = " << paragraph.size() << " characters. \n\n";
2017-12-24 01:30:49 +08:00
2019-08-21 10:10:08 +08:00
if (paragraph.empty())
2017-12-24 01:30:49 +08:00
{
2019-08-21 10:10:08 +08:00
cout << "\nThe paragraph is empty" << endl;
}
else
{
while (true)
{
2017-12-24 01:30:49 +08:00
string word;
cout << "Please enter the word you are searching for: ";
2019-08-21 10:10:08 +08:00
getline(cin, word);
2017-12-24 01:30:49 +08:00
cout << "Hello, your word is " << word << "!\n";
if (paragraph.find(word) == string::npos)
{
cout << word << " does not exist in the sentence" << endl;
}
else
{
2019-08-21 10:10:08 +08:00
cout << "The word " << word << " is now found at location " << paragraph.find(word) << endl
<< endl;
2017-12-24 01:30:49 +08:00
}
system("pause");
}
2019-08-21 10:10:08 +08:00
}
2017-12-24 01:30:49 +08:00
}