What do I have to do so that you can enter a whole sentence and that the program recognizes a certain word inside the code? If I, for example, enter the sentence: "I hope it won't rain today" (using getline(cin, string*)), how do I write: if the string contains the word rain,...?
std::istringstream is( "I hope it won't rain today" );
auto x = std::find( std::istream_iterator<std::string>( is ),
std::istream_iterator<std::string>(),
"rain" );
if ( x != std::istream_iterator<std::string>() ) std::cout << *x << std::endl;
I tried to copy your code and run it, but it didnt work. There were alot of error messages. For example missing initialisers or unrecognized istream_iterator. So it didnt work. Could you or someone else check whats wrong?
const std::string str = "Hopefully it won't rain today; I don't want to get wet." ;
// is the word 'hopefully' present in the string?
// is the word 'hope' present in the string?
// are the words 'today' and 'wet' present in the string?
// is 'today;' a word?
#include <cctype>
#include <iostream>
#include <string>
bool contains_word(const std::string& sentence, const std::string& word)
{
size_t pos = 0;
// Find the start of 'word' in 'sentence'.
while ((pos = sentence.substr(pos).find(word)) != std::string::npos) {
// Make sure it's actually a word (e.g. "rain" could be part of "drain").
// isalpha checks if the given character is in the alphabet. Here we check
// if the characters before and after 'word' are alphabet characters. If neither
// of them is an alphabet character then we've found a whole word.
if (!(isalpha(sentence[pos - 1])) || !(isalpha(sentence[pos + word.size() + 1])))
returntrue;
}
// If we get to here then we didn't find any instance of 'word' that was a whole word.
returnfalse;
}
The other methods of finding 'word' in 'sentence' don't take into account that the word could be part of another word (like 'rain' in 'drain'), in which case it wouldn't really be a word at all.
Now you can just do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main()
{
std::string sentence, word;
// Get the sentence to search.
std::cout << "Sentence: " << std::flush;
std::getline(std::cin, sentence);
// Get the word to search for.
std::cout << "Word: " << std::flush;
std::getline(std::cin, word);
// Say whether the sentence contains the word.
if (contains_word(sentence, word))
std::cout << "The sentence contains the word." << std::endl;
else
std::cout << "The sentence does not contain the word." << std::endl
return 0;
}
Lastly, you might want to change "const std::string&" to just "std::string" if you haven't learned about references yet.
You are right chrisname! That is what Ill do. But first, there is a small problem:
I am doing all this inside a class and not inside the main.cpp . I placed the bool into my main void-function and ran the program, but there were these errors:
expected } at end of input
(There is definitely no } missing!)
a function definition is not allowed here before { token
Yeah, it's one way to fake classes (since Javascript lacks them as it has a different way of implementing OOP, but sometimes you might want to use classes anyway). You can do something like this:
so since the problem was solved i am going to hijack the thread for a little bit to ask some javascript questions. i just got into javascript yesterday but ive got the basics. question one what is "this" question two: is javascript for the most part a collection of objects?
'this' in Javascript is similar to 'this' in C++, except it's a reference instead of a pointer. When an object uses 'this' in many OOP languages (Python uses 'self' instead but it's otherwise the same) it simply refers to that object. For example, in C++: