473,320 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

counting of all kinds of characters

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
8 5243
gpraghuram
1,275 Expert 1GB
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
xiaolim
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
1,275 Expert 1GB
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
xiaolim
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
1,275 Expert 1GB
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
xiaolim
18
hmm.. then what function should i use?
Aug 5 '08 #7
JosAH
11,448 Expert 8TB
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
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

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

Similar topics

2
by: Srinath Avadhanula | last post by:
Hello, I am wondering if there is a way of counting graphemes (or glyphs) in python. For example, in the following string: u'\u0915\u093e\u0915' ( or equivalently, u"\N{DEVANAGARI LETTER...
5
by: Matt | last post by:
Alright, so I'm a little confused here...what exactly does this do? I've run it and it doesn't display anything, so I've typed some things into it, to see if it'd do something then, but to no...
1
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
3
by: lord trousers | last post by:
I'm currently replacing the Quake 3 game code (not the rendering, sound, or collision detection pieces) with Python. I've now successfully loaded Python modules and made callbacks to them, rendered...
14
by: Dan | last post by:
Is this discouraged?: for line in open(filename): <do something with line> That is, should I do this instead?: fileptr = open(filename) for line in fileptr: <do something with line>
4
by: bigbagy | last post by:
Notes The programs will be compiled and tested on the machine which runs the Linux operating system. V3.4 of the GNU C/C++ compiler (gcc ,g++) must be used. A significant amount coding is...
7
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27...
3
by: arnuld | last post by:
this is an example programme that counts lines, words and characters. i have noticed one thing that this programme counts space, a newline and a tab as a character. i know: 1. a newline is...
3
by: majna | last post by:
I have character counter for textarea wich counting the characters. Special character needs same place as two normal characters because of 16-bit encoding. Counter is counting -2 when special...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.