Search This Blog

Friday, 1 June 2018

Introduction to Programming CS201 - Progrmming Lab 5 - Week 7

#include<iostream>
//#include<conio.h>
//#include <ctype.h>
#include<fstream>

using namespace std;

int main()
{
      char id[10];     
      char  name[50];
      char  address[100];
      ofstream outFile; // Handle for the input file
      ifstream inFile;
      int i = 0, lc = 0, uc = 0, dig = 0, ws = 0, pun = 0;
     
      //Getting the values of id, name and address from the user
      /////////////////////////////////////
      cout<<"Enter your id:";
      gets(id);

      cout<<"Enter your name:";
      gets(name);

      cout<<"Enter your address:";
      gets(address);
      /////////////////////////////////////
     
     
   

     //////////////////////////////////////
     //// Opening the file and write the value of id, name and address in the text file "PersonalInfo.tx" //////

     char FileName[] = "PersonalInfo.txt"; // The file is created in the current directory
     outFile.open(FileName, ios::out); // Opening the file
     // checking that file is successfully opened or not
     if (!outFile)
     {
     cout << "Can't open input file named " << FileName << endl;
     exit(1);
     }
     // Writing into the file
     outFile<<id<<name<<address;
     outFile.close();
     /////////////////////////////////////////


     ////// Opening the file and read all the contents of the PersonalInfo file and find the lowercase, uppercase, spaces, digits etc in it ///////
     char c;
     inFile.open(FileName); // Opening the file
     // checking that file is successfully opened or not
     if (!inFile)
     {
     cout << "Can't open input file named " << FileName << endl;
     exit(1);
     }
     // Reading the complete file character by character and printing on screen
     while ( (c = inFile.get()) != EOF){
         
     if (islower(c))
        lc++;
     else if (isupper(c))
          uc++;
     else if (isdigit(c))
          dig++;
     else if (isspace(c))
          ws++;
     else if (ispunct(c))
          pun++;
   
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Displying the lowercase, uppercasses, digits, white spaces, punctuation, others in the file on screen
cout <<endl<< "The file contains:"<< endl;
cout<< "lower case letters = "<< lc<< endl;
cout << "upper case letters = " << uc <<endl;
cout<< "digits = " << dig << endl;
cout<< "white space = "<< ws << endl;
cout<< "punctuation = "<< pun<< endl;



inFile.close();

system("pause");
}

No comments:

Post a Comment