hello
i want to enter a file and find a word at the file a whole and replace every on of it with anther word and this i was made it to find a word from a file can i modify it to replace a word.
One function should preferably do only one thing. Opening a file and operating on it are two things. Besides, what if the function should standard input instead of a file?
1 2 3 4 5 6 7 8 9 10 11 12 13
size_t foo( istream & fin, ostream & fout )
{
size_t count = 0; // replacement events
double value = 0.0;
while ( fin >> value ) {
if ( value < 7.0 ) {
value = 42.0;
++count;
}
fout << value << '\n';
}
return count;
}
Obviously, foo() is not about replacing words in text. It is just an example of an idea.
string::find() can tell a location of subsequence within a line, but "bobcat" contains a "cat". Is that ok?
If you want to match proper words, then the characters right before and after a match cannot be from alphabet. Thus, a line starts with "cat*", ends with "*cat", or contains "*cat*", where the '*' is whitespace or punctuation. For this purpose there are regular expressions in the C++11 standard.