How do I extract specific words from a string in a text file?
Mar 13, 2020 at 12:10am UTC
my assignment is to take a string from a text file and count the number of words in it. I've gotten that far but now we have to be able to take a certain number and display that number word to the console. Say my string is "Hello World" if I enter '2' it should give me the result "World". I'm not really sure how my function should look for this. This is my code so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
void getFileInfo(ifstream &inFile);
string words(ifstream &inFile);
int numOfWords(ifstream& inFile);
int main() {
ifstream inFile;
string sentence, fileName;
int numCount, word;
getFileInfo(inFile);
numCount = numOfWords(inFile);
inFile.clear(); // resets file pointer from the beginning
inFile.seekg( 0 );
sentence = words(inFile);
cout << sentence << ": has " << numCount << " words in it" << endl;
cout << "Enter a number to extract a word: " ;
cin >> word;
}
void getFileInfo(ifstream &inFile){
string fileName;
do {
cout << "Please enter the filename: " << endl;
cin >> fileName;
inFile.open(fileName.c_str());
if (!inFile){
cout << "Invalid try again" << endl;
}
}while (!inFile);
}
string words(ifstream &inFile){
string words, theWords;
getline(inFile, words);
cout << words;
return theWords;
}
int numOfWords(ifstream& inFile){
string fileName, words, str;
int numCount =0;
while (inFile >> words){
++numCount;
}
return numCount;
}
Mar 13, 2020 at 12:19pm UTC
1 2
sentence = words(inFile);
cout << sentence << ": has " << numCount << " words in it" << endl;
¿what does the `words()' function do?
> take a certain number and display that number word to the console
> I'm not really sure how my function should look for this.
«pass me the third book on the shelf»
¿can you write a pseudocode for that?
Mar 13, 2020 at 2:28pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void getFileInfo(ifstream& inFile);
void words(ifstream& inFile);
int numOfWords(ifstream& inFile);
string numWord(ifstream& inFile, int a);
int main() {
ifstream inFile;
string sentence, fileName;
int numCount, word;
getFileInfo(inFile);
numCount = numOfWords(inFile);
inFile.clear(); // resets file pointer from the beginning
inFile.seekg(0);
words(inFile);
cout << ": has " << numCount << " words in it" << endl;
cout << "Enter a number to extract a word: " ;
cin >> word;
inFile.clear(); // resets file pointer from the beginning
inFile.seekg(0);
string test = numWord(inFile, word);
cout << test;
return 0;
}
void getFileInfo(ifstream& inFile) {
string fileName;
do {
cout << "Please enter the filename: " << endl;
cin >> fileName;
inFile.open(fileName.c_str());
if (!inFile) {
cout << "Invalid try again" << endl;
}
} while (!inFile);
}
void words(ifstream& inFile) {
string words, theWords;
while (getline(inFile, words)) {
cout << words;
}
}
int numOfWords(ifstream& inFile) {
string fileName, words, str;
int numCount = 0;
while (inFile >> words) {
++numCount;
}
return numCount;
}
string numWord(ifstream& inFile, int a) {
string fileName, words;
int numCount = 0;
while (inFile >> words) {
++numCount;
if (numCount == a) {
return words;
}
}
return "Word not found" ;
}
Last edited on Mar 13, 2020 at 2:29pm UTC
Topic archived. No new replies allowed.