C++ Exercise Problem: Find word in text that the user has entered
Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string text, ord;
cout << "Type a sentence: ";
getline(cin, text);
cout << "Type a word: ";
getline(cin, ord);
// Find the first letter in the word
int pos = text.find(ord.at(0), 0);
// Find the last letter in the word
int ord_langd = ord.length();
string ordet;
// Get word
ordet = text.substr(pos, ord_langd);
// Compare the text sentence to see
// if the word the user entered exists in it
// This is the problem that I struggle with.
if () { // I can't complete the if statement
cout << "Word does not exist" << endl;
}
else {
cout << "Index: " << ord.at(0) << " " << pos << endl;
cout << "Word: " << ordet << endl;
}
return 0;
}
I want the user to be able to input a full sentence like "How are you" and then the user is asked to enter a word "are". If the word "are" is in the sentence then the program should tell the user. If the word "are" isn't in the sentence then the program should tell the user that the word doesn't exist within the string.
Example:
Type a sentence: How are you
Type a word: are
The word "are" exists in your sentence
Example:
Type a sentence: How are you
Type a word: bye
The word "bye" doesn't exist in your sentence
I hope I'm clear enough so that you get what I mean. Otherwise ask me to clarify.