Write a program in C++ that reads some text entered through the keyboard till the end of
file (eof) character is entered. The words in the text are written to different text files as per
the following conditions:
• The words beginning with any of the lowercase vowels (a, e, i, o, u) are written to
a file fileV.txt.
• The words beginning with a digit (0 – 9) are written to a file fileD.txt.
• All other words are written to a file fileRest.txt.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string word;
ofstream fvowels("fileV.txt");
ofstream fdigit("fileD.txt");
ofstream frest("fileRest.txt");
while (cin >> word) {
if (word[0] == 'a' || word[0] == 'e' || word[0] == 'i' ||
word[0] == 'o' || word[0] == 'u') {
fvowels << word << endl;
}
else if (word[0] >= '0' && word[0] <= '9') {
fdigit << word << endl;
}
else {
frest << word << endl;
}
}
return 0;
}
Comments
Leave a comment