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:
Sample input: Buy 1 Kg apples And 17 oranges immediately ^Z
Sample output:
fileV.txt: apples oranges immediately
fileD.txt: 1 17
fileRest.txt: Buy Kg And
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include<vector>
using namespace std;
/*
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.
Sample input: Buy 1 Kg apples And 17 oranges immediately ^Z
Sample output:
fileV.txt: apples oranges immediately
fileD.txt: 1 17
fileRest.txt: Buy Kg And
*/
string S;
vector<string> k;
vector<string> splitString(string str, string delimiter)
{
vector <string> result;
size_t pos=0;
string token;
while((pos = str.find(delimiter))!= std::string::npos)
{
token = str.substr(0,pos);
result.push_back(token);
str.erase(0,pos+delimiter.length());
}
if(!str.empty()) result.push_back(str);
return(result);
}
int main()
{
string S,z,str;
ifstream outfile_1,outfile_2,outfile_3;
int n,i;
char x,y;
string Name1 = "H:\\fileV.txt";
string Name2 = "H:\\fileD.txt";
string Name3 = "H:\\fileRest.txt";
outfile_1.open(Name1.c_str());
outfile_2.open(Name2.c_str());
outfile_3.open(Name3.c_str());
cout<<"\n\tEnter string: "; getline(cin,S);
k = splitString(&S[0]," ");
for(i=0;i<k.size();i++)
{
z = k[i].c_str();
z[0] = tolower(z[0]);
if(z[0]=='a' || z[0] == 'e' || z[0] == 'i' || z[0]=='o' || z[0]=='u')
{
outfile_1<<k[i]<<" ";
}
else
if(z[0]>='0' && z[0]<='9') outfile_2<<k[i]<<" ";
else outfile_3<<k[i]<<" ";
}
outfile_1.close();
outfile_2.close();
outfile_3.close();
return(0);
}
Comments
Leave a comment