I have an exercise in which I need to find words in a text file starting with user input symbol. I also need to determine in which line that word is and output that in text different text file.
I managed to write code to output words starting with symbol and count word's position in text, but i cannot figure out how to count in which line that word is. Also i need to find those words which have symbols like ?
!
etc. not only ' '
For example if i wanna find words starting with c
then my program finds only "cerebral, cortex, could, create" but not "construct, capable, computers" from my example input which is below my code.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream input;
fstream output;
string word, line;
char startOfWord;
cout << "I wanna find words starting with symbol: \n";
cin >> startOfWord;
int lineNumber = 0;
input.open("f.txt");
output.open("f1.txt");
while (!input.eof()) {
input >> word;
lineNumber++;
if (word.length() > 40) {
continue;
}
if (word[0] == startOfWord) {
output << word << ' ' << lineNumber << '\n';
}
}
input.close();
output.close();
return 0;
}
Example input: user wanna find words starting with a
.
f.txt:
A Stanford University project to?construct a model
of the cerebral cortex in silicon could help scientists
gain a better understanding of the brain, in order to
create more,capable.computers and advanced
neural prosthetics.
Output: f1.txt
a 1
a 3
and 4
advanced 4