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

Discussion in 'C Programming' started by Umesh, Feb 1, 2007.

1. ### UmeshGuest

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

Umesh, Feb 1, 2007

2. ### Clever MonkeyGuest

Umesh wrote:
> Plese help. Is there any software by which we can do that?
>

rely on the text in the Subject to always be obvious.

"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.

Clever Monkey, Feb 1, 2007

3. ### UmeshGuest

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?

Umesh, Feb 1, 2007
4. ### Walter RobersonGuest

In article <>,
Umesh <> wrote:
>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++).

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?
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers

Walter Roberson, Feb 1, 2007
5. ### UmeshGuest

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

Walter Roberson wrote:
> In article <>,
> Umesh <> wrote:
> >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++).
>
> 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?
> --
> "No one has the right to destroy another person's belief by
> demanding empirical evidence." -- Ann Landers

Umesh, Feb 1, 2007
6. ### osmiumGuest

"Umesh" <> wrote in message
news:...
>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?

There probably is such a program but it most likely would be faster to write
such a program than to find, download, figure out how to use/ determine that
you *can't* use, such a program, and then use it, or start over.

Set up an array of 26 char and clear it. Then read the file,incrementing
the count in the appropriate array element when a character is received.

osmium, Feb 1, 2007
7. ### Dave SteffenGuest

"Umesh" <> writes:

> 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.

----------------------------------------------------------------------
Dave Steffen, Ph.D. Disobey this command!
Software Engineer IV - Douglas Hofstadter
Numerica Corporation
dg@steffen a@t numerica d@ot us (remove @'s to email me)

Dave Steffen, Feb 1, 2007
8. ### Mark PGuest

Umesh wrote:
> 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.

Mark P, Feb 1, 2007
9. ### UmeshGuest

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.

Dave Steffen wrote:
> "Umesh" <> writes:
>
>
> > 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.
>
> ----------------------------------------------------------------------
> Dave Steffen, Ph.D. Disobey this command!
> Software Engineer IV - Douglas Hofstadter
> Numerica Corporation
> dg@steffen a@t numerica d@ot us (remove @'s to email me)

Umesh, Feb 1, 2007
10. ### Andre KosturGuest

"Umesh" <> wrote in
news::

> 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.

How much are you paying?

See the C++ FAQ, section 5.3:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.3

Do it yourself. If you get stuck, ask a specific question.

Andre Kostur, Feb 1, 2007
11. ### Ian CollinsGuest

Umesh wrote:
> 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.

> Dave Steffen wrote:
>
>>"Umesh" <> writes:
>>
>>
>>
>>>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.
>>

--
Ian Collins.

Ian Collins, Feb 1, 2007
12. ### Walter RobersonGuest

In article <>,
Umesh <> top-posted:

>Walter Roberson wrote:
>> In article <>,
>> Umesh <> wrote:
>> >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?

>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
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

--
All is vanity. -- Ecclesiastes

Walter Roberson, Feb 1, 2007
13. ### Clever MonkeyGuest

Umesh wrote:
> 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.

Clever Monkey, Feb 1, 2007
14. ### Default UserGuest

Re: How to calculate the percentage of each character in a text file? - TPA

Umesh wrote:

> For simplicity assume that all 26 inputs are in lower case.

with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>

Default User, Feb 1, 2007
15. ### Charlton WilburGuest

>>>>> "U" == Umesh <> writes:

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

--
Charlton Wilbur

Charlton Wilbur, Feb 1, 2007
16. ### Mark McIntyreGuest

On 1 Feb 2007 10:58:15 -0800, in comp.lang.c , "Umesh"
<> wrote:

>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

Mark McIntyre, Feb 1, 2007
17. ### CBFalconerGuest

Umesh wrote:
>
> Plese help. Is there any software by which we can do that?

Yes.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

CBFalconer, Feb 1, 2007
18. ### CBFalconerGuest

Umesh wrote:
>
> 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?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

CBFalconer, Feb 1, 2007
19. ### CBFalconerGuest

Umesh wrote:
>
> 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.

--
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://members.fortunecity.com/nnqweb/> (newusers)

CBFalconer, Feb 1, 2007
20. ### Mark PGuest

Umesh top-posted, and I fixed:
> Dave Steffen wrote:
>> "Umesh" <> writes:
>>
>>
>>> 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.
>>

> 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?

Mark P, Feb 1, 2007