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. -
-
#include <iostream>
-
#include <cstring>
-
#include <fstream>
-
#include <conio.h>
-
#include<stdio.h>
-
#include <string.h>
-
-
using namespace std;
-
-
-
int main()
-
{
-
-
ifstream fin;
-
ofstream fout;
-
string filename;
-
-
cout << "Welcome\n" << endl;
-
cout << "This programe will analyze the file content &" << endl;
-
cout << "compute the statistics of the file you input.\n\n\n\n\n" << endl;
-
system("pause");
-
system("cls");
-
-
-
do
-
{
-
cout << "Enter input data file name:\n";
-
cin >> filename; // Get the file name.
-
fin.open(filename.c_str()); // Convert to C-string and open it.
-
if (!fin)
-
{ // Will fail if didn't exist.
-
cout << "Unable to open " << filename << endl;
-
cin.get();
-
system("cls");
-
-
}
-
} while(!fin);
-
-
-
-
-
char next, ch; // counting of character where i was stucked
-
-
int letters = 0;
-
int digits = 0;
-
int upper = 0;
-
int lower = 0;
-
int space = 0;
-
int punctuation = 0;
-
int others = 0;
-
-
fin.get(next);
-
while (!fin.eof( ))
-
{
-
if (next == '-')
-
ch = ' ';
-
else if (isupper(next))
-
ch = tolower(next);
-
else if (isdigit(next))
-
ch = '*';
-
else ch = next;
-
-
fout << ch;
-
-
// Increment your count here while you are reading
-
letters++;
-
fin.get(next);
-
}
-
-
// Print out the number of letters counted.
-
cout << "Total Number of Letters: " << letters << endl;
-
cout << "" << endl;
-
cout << "Total Number of Uppercase: " << upper << endl;
-
cout << "Total Number of Lowercase: " << lower << endl;
-
cout << "Total Number of Digits: " << digits << endl;
-
cout << "Total Number of Space: " << space << endl;
-
cout << "Total Number of Punctuation: " << punctuation << endl;
-
cout << "Total Number of other characters: " << others << endl;
-
cout << "" << endl;
-
-
-
system("pause");
-
-
return 0;
-
}
-
-
-
so, i would like to count other characters as well as what is shown in the last part. any guide or help? thanks
8 5243
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
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.
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
hmm...i tried again and the code looks like this now: -
-
#include <iostream>
-
#include <cstring>
-
#include <fstream>
-
#include <conio.h>
-
#include <stdio.h>
-
#include <string.h>
-
#include <cstdlib>
-
-
using namespace std;
-
-
-
int main(void)
-
{
-
-
ifstream fin;
-
ofstream fout;
-
string filename;
-
-
cout << "Welcome\n" << endl;
-
cout << "This programe will analyze the file content &" << endl;
-
cout << "compute the statistics of the file you input.\n\n\n\n\n" << endl;
-
system("pause");
-
system("cls");
-
-
-
do
-
{
-
cout << "Enter input data file name:\n";
-
cin >> filename;
-
cout << "\n"; // Get the file name.
-
fin.open(filename.c_str()); // Convert to C-string and open it.
-
if (!fin)
-
{ // Will fail if didn't exist.
-
cout << "Unable to open " << filename << endl;
-
cin.get();
-
system("cls");
-
-
}
-
} while(!fin);
-
-
-
-
-
char next;
-
int characters = 0;
-
int digits = 0;
-
int upper = 0;
-
int lower = 0;
-
int space = 0;
-
int eospm = 0;
-
int others = 0;
-
-
-
while((next = fin.get()) != EOF)
-
{
-
//calculate total numbers of characters including space ---> it doesnt count all the characters, what should i use to count every characters including space?
-
if(isalnum(next))
-
characters++;
-
-
//calculate total numbers of digits ---> correct
-
if(isdigit(next))
-
digits++;
-
-
//calculate total numbers of uppercase ---> correct
-
else if(isupper(next))
-
upper++;
-
-
//calculate total numbers of lowercase ---> correct
-
else if(islower(next))
-
lower++;
-
-
//calculate total numbers of space ---> this 1 doesnt count the correct whitespace either.
-
else if(isspace(next))
-
space++;
-
-
//calculate total numbers of punctuation ---> correct
-
else if(ispunct(next))
-
others++;
-
-
}
-
-
// Print out what is counted.
-
cout << "Total Number of Characters: " << characters << endl;
-
cout << "" << endl;
-
cout << "Total Number of Uppercase: " << upper << endl;
-
cout << "Total Number of Lowercase: " << lower << endl;
-
cout << "Total Number of Digits: " << digits << endl;
-
cout << "Total Number of Space: " << space << endl;
-
cout << "Total Number of End-of-Sentence Punctuation Marks: " << eospm << endl;
-
cout << "Total Number of Other Characters: " << others << endl;
-
cout << "" << endl;
-
-
-
system("pause");
-
-
return 0;
-
}
-
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?
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
hmm.. then what function should i use?
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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>
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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....
|
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...
| |