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

C

CBFalconer

Mark said:
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.

He can take a glass of water and drop in a spoonful of salt. That
will give him a saline solution, which fits his specification
accurately.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
M

Malcolm McLean

Walter Roberson said:
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"}}'
That is sort of thing that gets Perl a bad name.

C is a lot less concise, but at least you can read it.

MiniBasic would be better still. Unfortunately there is no way of detecting
end of input.

http://www.personal.leeds.ac.uk/~bgy1mm
 
I

Ian Collins

Malcolm said:
Walter Roberson said:
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"}}'

That is sort of thing that gets Perl a bad name.
Does it? I thought Perl was intended to be a write only language :)
 
M

Malcolm McLean

Ian Collins said:
Malcolm said:
Walter Roberson said:
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"}}'

That is sort of thing that gets Perl a bad name.
Does it? I thought Perl was intended to be a write only language :)
It might have been deliberately obfuscated, or it might be one of his
workaday scripts. With Perl you cannot tell.
 
C

Charlton Wilbur

>> 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"}}'

MML> That is sort of thing that gets Perl a bad name.

MML> C is a lot less concise, but at least you can read it.

Consider the Obfuscated C contest, please, and the fact that both Mr
Roberson's answer and mine were written as one-liners; in my case, I
intentionally aimed for terseness as well, and it looks to me as if Mr
Roberson aimed for the same thing.

C and Perl share the property that they can be as readable or
unreadable as the author desires; the fact that there are many idiot
programmers who can't write one or the other clearly is not a
condemnation of the languages -- or would you rather have us all
writing COBOL?

Charlton
 
M

Martin Ambuhl

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?

Since this is a trivial exercise given to beginning programming
students, it is not economically sound to offer such software. It would
cost more to produce copies than the market would bear.
 
C

Christopher Layne

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.

We're not here to work for you, clown.
 
R

Richard

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

What following links?

If you are going to net nanny people please include the material you
wish them to read properly so that the post is usenet compliant.
 
R

Richard Heathfield

Richard said:
What following links?

They're in his sig block.
If you are going to net nanny people please include the material you
wish them to read properly so that the post is usenet compliant.

He did. Sig blocks are Usenet compliant.
 
R

Richard Bos

Malcolm McLean said:
Walter Roberson said:
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"}}'
That is sort of thing that gets Perl a bad name.

Oh, no. That's tame compared to some I've seen. At least if you indent
it properly it's halfway readable.
C is a lot less concise, but at least you can read it.

If properly written. If not, not.
MiniBasic would be better still. Unfortunately there is no way of detecting
end of input.

That's one fundamental reason why Mini- or more or less any Basic would
not be better still. If you want to do this kind of small text problem,
use either _well_-written Perl, or a proper specialist language such as
Icon.

Richard
 
M

Malcolm McLean

Richard Bos said:
That's one fundamental reason why Mini- or more or less any Basic would
not be better still. If you want to do this kind of small text problem,
use either _well_-written Perl, or a proper specialist language such as
Icon.
Not fundamental. Since MinBasic comes as source it would be trival to add an
"ONEOF" construct. The whole point of the language is that the interpreter
is easy to modify, after all.

I didn't put it in as standard simply because microcomputer BASICs generally
don't have it, though it was something I considered.

I've never heard of Icon. There are way too many programming languages
sloshing around that do essentially the same thing but slightly differently.
Eg Fortran and C, or Java and C++.
Perl is one of my least favourites. As a language implementor myself I've
got to say that I admire Larry Wall's technical skill and marketing success.
The core problem is that regular expressions are too complicated for humans
to read, and generally not what is wanted. I want names starting with,
ending with, containing the sequence, of sounding like, not matching 1-5
digits accidentally 0-5 because I misused the * operator.
 
L

Lane Straatman

Richard Heathfield said:
Richard said:


They're in his sig block.


He did. Sig blocks are Usenet compliant.
My answer for this question was so stupid that it amuses. With the
percentage, I thought he meant "modulo," and I wrote a prog that looks at
the chars from a to z, modulo e:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char index, mod;
mod = 'e';
for( index = 'a'; index <= 'z'; ++ index)
{
printf( "%c %c %hhd %hhd\n ", index, index % mod, index, index %
mod);
}
system("PAUSE");
return 0;
}
What is the character that makes the beep? LS
 
C

CBFalconer

Lane said:
.... snip ...

My answer for this question was so stupid that it amuses. With the
percentage, I thought he meant "modulo," and I wrote a prog that
looks at the chars from a to z, modulo e:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char index, mod;
mod = 'e';
for( index = 'a'; index <= 'z'; ++ index)
{
printf( "%c %c %hhd %hhd\n ", index, index % mod, index, index %+ mod);
}
system("PAUSE");
return 0;
}
What is the character that makes the beep? LS

On your system:

for (ch = 'a'; ch <= 'z'; ch++)
if ('\a' == (ch % 'e')) putchar(ch);

f'ups set. Crossposts between C and C++ are not productive.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
J

Jerry Coffin

[ ... ]
I've never heard of Icon. There are way too many programming languages
sloshing around that do essentially the same thing but slightly differently.
Eg Fortran and C, or Java and C++.

Icon most assuredly does not fit that description. It's a descendent
(sort of) of the SNOBOL series of languages. While Icon may not be quite
as different as SNOBOL, it's still definitely not just the same old
thing with a minor variation in syntax.
Perl is one of my least favourites. As a language implementor myself I've
got to say that I admire Larry Wall's technical skill and marketing success.
The core problem is that regular expressions are too complicated for humans
to read, and generally not what is wanted. I want names starting with,
ending with, containing the sequence, of sounding like, not matching 1-5
digits accidentally 0-5 because I misused the * operator.

Like PERL (, AWK, etc.), Icon has pattern matching as a fundamental part
of the language. In PERL, however, the patterns are basically defined
entirely as strings in a rather difficult to read syntax. While it's
open to argument that Icon's syntax could be improved, I'd say it's a
lot more readable than a Regex anyway.

If you care to look at it, the C source code to an (the?) Icon
implementation is available from the University of Arizona. Start from:

http://www.cs.arizona.edu/icon/
 
R

Richard Bos

Malcolm McLean said:
I've never heard of Icon.
The core problem [with Perl - RLB] is that regular expressions are too
complicated for humans to read, and generally not what is wanted. I want
names starting with, ending with, containing the sequence, of sounding like,
not matching 1-5 digits accidentally 0-5 because I misused the * operator.

Congratulations. You've just described perfectly why Icon is a better
language for you than Perl (and than Basic, BTW, but that speaks for
itself).

Richard
 

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

No members online now.

Forum statistics

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

Latest Threads

Top