By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,137 Members | 2,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,137 IT Pros & Developers. It's quick & easy.

counting of all kinds of characters

P: 18
i making a simple program to count the different kinds of characters in a text file and then display them out, however i only manage to count the total numbers of characters.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream> 
  3. #include <cstring>  
  4. #include <fstream>
  5. #include <conio.h>
  6. #include<stdio.h>
  7. #include <string.h>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. int main()
  13. {
  14.  
  15.     ifstream fin;
  16.     ofstream fout;
  17.     string filename;
  18.  
  19.     cout << "Welcome\n" << endl;
  20.     cout << "This programe will analyze the file content &" << endl;
  21.     cout << "compute the statistics of the file you input.\n\n\n\n\n" << endl;
  22.     system("pause");
  23.     system("cls");
  24.  
  25.  
  26.     do 
  27.        {
  28.         cout << "Enter input data file name:\n";
  29.         cin >> filename;     // Get the file name.
  30.         fin.open(filename.c_str());  // Convert to C-string and open it.
  31.         if (!fin) 
  32.              {          // Will fail if didn't exist.
  33.                 cout << "Unable to open " << filename << endl;
  34.                 cin.get();
  35.                 system("cls");
  36.  
  37.              } 
  38.         } while(!fin);
  39.  
  40.  
  41.  
  42.  
  43.     char next, ch;    // counting of character where i was stucked
  44.  
  45.     int letters = 0;
  46.     int digits = 0;
  47.     int upper = 0;
  48.     int lower = 0;
  49.     int space = 0;
  50.     int punctuation = 0;
  51.     int others = 0;
  52.  
  53.     fin.get(next);
  54.     while (!fin.eof( ))
  55.     {
  56.         if  (next == '-')
  57.             ch = ' ';
  58.         else if (isupper(next))
  59.             ch = tolower(next);
  60.         else if (isdigit(next))
  61.             ch = '*';
  62.         else ch = next;
  63.  
  64.         fout << ch;
  65.  
  66.         // Increment your count here while you are reading
  67.         letters++;
  68.         fin.get(next);  
  69.     }
  70.  
  71.     // Print out the number of letters counted.
  72.     cout << "Total Number of Letters: " << letters << endl; 
  73.     cout << "" << endl;
  74.     cout << "Total Number of Uppercase: " << upper << endl; 
  75.     cout << "Total Number of Lowercase: " << lower << endl; 
  76.     cout << "Total Number of Digits: " << digits << endl; 
  77.     cout << "Total Number of Space: " << space << endl; 
  78.     cout << "Total Number of Punctuation: " << punctuation << endl; 
  79.     cout << "Total Number of other characters: " << others << endl;      
  80.     cout << "" << endl;
  81.  
  82.  
  83.        system("pause");
  84.  
  85. return 0;
  86. }
  87.  
  88.  
  89.  


so, i would like to count other characters as well as what is shown in the last part. any guide or help? thanks
Aug 5 '08 #1
Share this Question
Share on Google+
8 Replies


gpraghuram
Expert 100+
P: 1,275
Where r u miantainig the count of upper case and lower case letters?
You are having only a common variable which gets incremented with every character.

raghu
Aug 5 '08 #2

P: 18
ya i know, but i having problem coding them as to count other characters as well as i don't know where to start with.
Aug 5 '08 #3

gpraghuram
Expert 100+
P: 1,275
ya i know, but i having problem coding them as to count other characters as well as i don't know where to start with.
Then you shuld
1) first find whether the character is a lowercase(If so increment a avariable)
2)then find whether the character is a uppercase(If so increment a avariable)

Why u are calling tolower and toupper functions in your code if you want to find uppercase and lower case count?

Raghu
Aug 5 '08 #4

P: 18
hmm...i tried again and the code looks like this now:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream> 
  3. #include <cstring>  
  4. #include <fstream>
  5. #include <conio.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <cstdlib>
  9.  
  10. using namespace std;
  11.  
  12.  
  13. int main(void)
  14. {
  15.  
  16.     ifstream fin;
  17.     ofstream fout;
  18.     string filename;
  19.  
  20.     cout << "Welcome\n" << endl;
  21.     cout << "This programe will analyze the file content &" << endl;
  22.     cout << "compute the statistics of the file you input.\n\n\n\n\n" << endl;
  23.     system("pause");
  24.     system("cls");
  25.  
  26.  
  27.     do 
  28.        {
  29.         cout << "Enter input data file name:\n";
  30.         cin >> filename;
  31.         cout << "\n";    // Get the file name.
  32.         fin.open(filename.c_str());  // Convert to C-string and open it.
  33.         if (!fin) 
  34.              {          // Will fail if didn't exist.
  35.                 cout << "Unable to open " << filename << endl;
  36.                 cin.get();
  37.                 system("cls");
  38.  
  39.              } 
  40.         } while(!fin);
  41.  
  42.  
  43.  
  44.  
  45.     char next;
  46.     int characters = 0;
  47.     int digits = 0;
  48.     int upper = 0;
  49.     int lower = 0;
  50.     int space = 0;
  51.     int eospm = 0;
  52.     int others = 0;
  53.  
  54.  
  55.     while((next = fin.get()) != EOF)
  56.     {
  57.           //calculate total numbers of characters including space    ---> it doesnt count all the characters, what should i use to count every characters including space?
  58.           if(isalnum(next))
  59.           characters++;
  60.  
  61.           //calculate total numbers of digits   ---> correct
  62.           if(isdigit(next))
  63.           digits++;
  64.  
  65.           //calculate total numbers of uppercase   ---> correct
  66.           else if(isupper(next))
  67.           upper++;
  68.  
  69.           //calculate total numbers of lowercase   ---> correct
  70.           else if(islower(next))
  71.           lower++;
  72.  
  73.           //calculate total numbers of space    ---> this 1 doesnt count the correct whitespace either.
  74.           else if(isspace(next))
  75.           space++;
  76.  
  77.           //calculate total numbers of punctuation   ---> correct
  78.           else if(ispunct(next))
  79.           others++;         
  80.  
  81.     }
  82.  
  83.     // Print out what is counted.
  84.     cout << "Total Number of Characters: " << characters << endl; 
  85.     cout << "" << endl;
  86.     cout << "Total Number of Uppercase: " << upper << endl; 
  87.     cout << "Total Number of Lowercase: " << lower << endl; 
  88.     cout << "Total Number of Digits: " << digits << endl; 
  89.     cout << "Total Number of Space: " << space << endl; 
  90.     cout << "Total Number of End-of-Sentence Punctuation Marks: " << eospm << endl; 
  91.     cout << "Total Number of Other Characters: " << others << endl;      
  92.     cout << "" << endl;
  93.  
  94.  
  95.        system("pause");
  96.  
  97. return 0;
  98. }
  99.  
alright, i think i am close to what i want, but i wanted to count all the characters in the file, End-of-Sentence Punctuation Marks( those punct in the end of a sentence ) and the number of white space in between the sentence. how would i do it?
Aug 5 '08 #5

gpraghuram
Expert 100+
P: 1,275
Then you should add another else if statement to check for \n character and accordingly else if statements with the characters you want to count.

Thanks
Raghuram
Aug 5 '08 #6

P: 18
hmm.. then what function should i use?
Aug 5 '08 #7

Expert 10K+
P: 11,448
hmm.. then what function should i use?
Start reading what functions are there already in the C library; read up on the
ctype.h header file and start thinking; don't just blindly copy 'n paste code from
the internet.

kind regards,

Jos
Aug 5 '08 #8

P: 62
The easiest thing to do in this case is to just use an array, cast the characters as ints, and then increment the apropriate location in the array based on the the int derived from the char.

If necessary you can even shrink the size of the array and just shift the value derived based off of the char though this only works if you know that a certain subset of characters within a range will be used.

If you decide to do it this way just look for an ascii table and it should give you a feel for what is going on.

Edward
Aug 6 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.