How to calculate the percentage of each character in a text file?

C

Clever Monkey

Umesh said:
Plese help. Is there any software by which we can do that?
Please restate your real question in the body of your posting. Don't
rely on the text in the Subject to always be obvious.

The question you asked is:

"How to calculate the percentage of each character in a text file?"

Sounds like homework to me. What have you done so far? You are
essentially asking for a distribution of letters (and, perhaps,
punctuation) in a file, possibly in order to print a histogram and/or
report on how often such characters appears in the file.

Start by counting how many of each character you get. Compare that with
the total number of characters you have.

Hint: choose a language and post to that group only. There is some
overlap between C and C++, but they are really different beasts.
 
U

Umesh

I want to find out % of a, b, c, d ..,z in a text file. Suppose that
the text file is a story. Is there any software for this purpose?
 
W

Walter Roberson

I want to find out % of a, b, c, d ..,z in a text file. Suppose that
the text file is a story. Is there any software for this purpose?

Yes, there is software for that purpose, but No, I couldn't name
any immediately.

This sort of program is trivial to write in C (or C++).

A question about your specifications: should upper and lower
case characters be treated as equivilent? Should they be
counted individually? Your specification only mentioned the
lower case letters, so should upper case letters be silently
ignored?

Should the program make some kind of attempt to determine whether
a letter is part of a word, and ignore the ones that are not?
For example, if there is a phrase that "She lives at 36-E 9th St.",
then should the 'E' be counted (36-E is not a word), should
the 't' and 'h' of '9th' be counted, should the 'S' and 't'
of 'St.' be counted?
 
D

Dave Steffen

Please don't top post...
I need at least a solution first. Then I'll look for specifications.

*Snort*

If you don't know the specifications of your solution, how will you
know if you have one?

I think you're in the wrong group. If you're looking for an existing
tool, you need to find a newsgroup for your platform.
 
M

Mark P

Umesh said:
I need at least a solution first. Then I'll look for specifications.

That would be quite funny if it weren't written by someone who probably
intends to become a programmer. Please do humanity a favor and change
majors.
 
U

Umesh

For simplicity assume that all 26 inputs are in lower case. Now pl
write a program to input a text file and count number of a, b, c, ..,z
in the text file. Put is down in a output file. I'll do the rest.
 
I

Ian Collins

Umesh said:
For simplicity assume that all 26 inputs are in lower case. Now pl
write a program to input a text file and count number of a, b, c, ..,z
in the text file. Put is down in a output file. I'll do the rest.
Didn't you see the words "Please don't top post"?

What rest? Come on, give it a go a post your code. When you do, choose
either C or C++ and post to the appropriate group.
 
W

Walter Roberson

I need at least a solution first. Then I'll look for specifications.

Okay, here's a starting solution that meets all the specifications
you have given so far. It isn't in C or C++, but you have mostly
been asking about "software for this purpose", without appearing
to care much about the programming language.

perl -e 'undef $/; foreach (split(//,<>)) {if (/[a-z]/) {$N{$_}++;$T++}
}; END {foreach (keys(%N)) {print "$_ : $N{$_} ",100 * $N{$_}/$T, "\n"}}'

Sample output:

r : 13 6.04651162790698
a : 15 6.97674418604651
d : 11 5.11627906976744
y : 2 0.930232558139535
u : 6 2.7906976744186
h : 12 5.58139534883721
g : 11 5.11627906976744
f : 12 5.58139534883721
i : 22 10.2325581395349
t : 35 16.2790697674419
e : 20 9.30232558139535
n : 22 10.2325581395349
m : 2 0.930232558139535
v : 1 0.465116279069767
s : 7 3.25581395348837
l : 7 3.25581395348837
p : 3 1.3953488372093
c : 6 2.7906976744186
b : 2 0.930232558139535
z : 1 0.465116279069767
o : 5 2.32558139534884
 
C

Clever Monkey

Umesh said:
I want to find out % of a, b, c, d ..,z in a text file. Suppose that
the text file is a story. Is there any software for this purpose?
By Grabthar's Hammer, will you please quite context (and bottom-post
like the gods intended)...

Yes, there is software to do this. It is a classic awk problem, with
many, many published solutions. Check out "The Unix Programming
Environment" by Kernighan & Pike.

If you want it in C (or C++) then you are going to have to:

- pay for someone to do the work
- do it yourself, perhaps with some help

The first option requires money up front. The second requires that you,
you know, show a little initiative.
 
C

Charlton Wilbur

U> For simplicity assume that all 26 inputs are in lower case. Now
U> pl write a program to input a text file and count number of a,
U> b, c, ..,z in the text file. Put is down in a output file. I'll
U> do the rest.

perl -e 'undef $/; @c = split //, <>; $f{lc $_}++ for @c; print "$_: $f{$_}\n" for ("a".."z");'

Good luck.
(and apologies for line length longer than 80)

Charlton
 
M

Markus Svilans

Plese help. Is there any software by which we can do that?


In C++, one of the many ways to do this is:

Create a map where the key is char, and value is int (use std::map)
Create an int variable called "charCount" to keep track of the total
character count

Initialize charCount to zero
Initialize entries for 'a' through 'z' to zero

For each line of the file (use std::ifstream and std::getline)
Convert the line to lowercase
For each character in the line (ignoring ones outside the 'a' to
'z' range)
Increment the map entry by one
Increment charCount by one
End for
End for

Finally, iterate over the map entries, calculate and display
percentages based on the data in the map and charCount.

Regards,
Markus.
 
M

Mark McIntyre

I need at least a solution first. Then I'll look for specifications.

I hear this all the time at work, normally from clueless users who
have a deadline to meet and no idea what they're supposed to do. It
seems I'm supposed to be telepathic _and_ prescient, so I can work out
what their boss will want at a future date, code it, and then write
them a spec for their records.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
C

CBFalconer

Umesh said:
I want to find out % of a, b, c, d ..,z in a text file. Suppose that
the text file is a story. Is there any software for this purpose?

The answer is still 'yes'.
 
C

CBFalconer

Umesh said:
For simplicity assume that all 26 inputs are in lower case. Now pl
write a program to input a text file and count number of a, b, c, ..,z
in the text file. Put is down in a output file. I'll do the rest.

You are top-posting. Don't do that. Read the following links.

--
Some informative links:
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)
 
M

Mark P

Umesh top-posted, and I fixed:
For simplicity assume that all 26 inputs are in lower case. Now pl
write a program to input a text file and count number of a, b, c, ..,z
in the text file. Put is down in a output file. I'll do the rest.

Hurry up, Dave! He already said "pl". What more do you need?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top