Write a program that reads in data from an input file called

Write a program that reads in data from an input file called data In input. The file contains a list of characters. Your program should count the number of special characters that appear in that file output your results to a file. programming with C++

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

 Write a program that reads in data from an input file called data In input. The file contains a list of characters. Your program should count the number of spe
 Write a program that reads in data from an input file called data In input. The file contains a list of characters. Your program should count the number of spe

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site