Im very new to C++ and ive been struggling for quite a while trying to figure out how to do this problem. Basically, i need to read from a file and find all instances of an article ("a","A","an","aN","An","AN","the","The","tHe","thE","THe","tHE","ThE","THE")and then insert an adjective after that article. The adjective's capitalization must be based on the word originally in front of the article. For instance, if i found "a SHARK" i would need to make it "a HAPPY SHARK." Can anyone tell me what the best way to do this would be? So far I've scrapped a lot of ideas and this is what i have now, though i don't think i can do it this way:
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <sstream>
using namespace std;
void
usage(char *progname, string msg){
cerr << "Error: " << msg << endl;
cerr << "Usage is: " << progname << " [filename]" << endl;
cerr << " specifying filename reads from that file; no filename reads standard input" << endl;
}
int main(int argc, char *argv[])
{
string adj;
string file;
string line;
string articles[14] = {"a","A","an","aN","An","AN","the","The","tHe","thE","THe","tHE","ThE","THE"};
ifstream rfile;
cin >> adj;
cin >> file;
rfile.open(file.c_str());
if(rfile.fail()){
cerr << "Error while attempting to open the file." << endl;
return 0;
}
while(rfile.good()){
getline(rfile,line,'\n');
istringstream iss(line);
string word;
while(iss >> word){
for(int i = 0; i <= 14; i++){
if(word == articles[i]){
cout << word + " " << endl;
}else{
continue;
}
}
}
}
}
So far, pretty good, although if you need to handle an article at the end of a line, then you might be in trouble doing this line by line.
Anyway, ignoring that wrinkle for a second, after you've matched an article, then first you need to get the next word on which you need to base your capitalization. Then you need to create a new string version of your adjective that has the correct capitalization:
Circling back to the wrinkle we ignored. You probably don't want to do this line by line and then token by token because handling this special case will be ugly in your control. Instead, you probably want to do it token by token in a single loop.
So, you need to write a helper function or class that operates on the file and can give you the next token. (There probably is exactly such a class already in the STL, I'm not sure.) Anyway, using your I/O it might look something like:
And your main loop would then look like:
You probably want to output the rest of the input too?
First I propose you to use 3 auxiliary function to transform string cases. These will be usefull if you work a lot with text. Here they are based on
<algorithm>
but many other aproaches are possible:Then a utility function to clone the capitalisation of a word : it sets the adjective to lowercase or uppercase or capitalizes it(1 upper+rest lower) copying the case of the refernce word. It's robust enough to handle empty words, and words wich are not alaphanumeric:
All these functions do not change the original strings !
Now to the
main()
: I don't like having to manually put all the possible combination of upper and lowercase of the articles, so I work only uppercase.I don't like either to sequentially go through all possible articles for every word. If there would be many more articles, it would not be very performant ! So I prefer to use a
<set>
: