Write a program that reads in data from an input file called
Solution
// Program for read data from file and counts special chars & alphabets and then write to file
#include <iostream>
#include <fstream>
using namespace std;
int main () //Execution begins
{
char schars[]={\'<\',\'>\',\'=\',\'+\',\'-\',\'@\',\'{\',\'}\'}; //speciall chars intialization
char alphabets[]=\"abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\"; //alphabets intialization
int alpha=0,scount=0;
char tempc;
ifstream fin;
fin.open(\"wdatain.txt\", ios::in); //input file open for read data
char my_character ;
while (!fin.eof() ) // loop for reading data until file ends
{
fin.get(my_character);
for(int i=0;i<8;i++) //loop for checking special char or not,if speecial char increments scount
{
if (my_character==schars[i])
{
scount++;
}
}
for(int i=0;i<52;i++) //loop for checking alphabet char or not,if alphabet char increments scount
{
if (my_character==alphabets[i])
{
alpha++;
}
}
//cout << my_character;
}
ofstream myfile (\"example.txt\"); //file open for write output
if (myfile.is_open())
{
myfile << \"Number of special chars - \"<< scount <<endl; // write output
myfile << \"Number of alphabets - \" << alpha <<endl; // write output
myfile.close();
}
else cout << \"Unable to open file\";
cout << \"Number of special chars - \"<< scount <<endl;
cout << \"Number of alphabets - \" << alpha <<endl;
return 0;
}
//Program ends here
Note : Input file name \"wdatain.in\" and it contains some data.
Output : In file example.txt
Example output : Number of special chars - 2
Number of alphabets - 12

