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

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

  1. Umesh

    Umesh Guest

    Plese help. Is there any software by which we can do that?
     
    Umesh, Feb 1, 2007
    #1
    1. Advertising

  2. Umesh wrote:
    > 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.
     
    Clever Monkey, Feb 1, 2007
    #2
    1. Advertising

  3. Umesh

    Umesh Guest

    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
    #3
  4. 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++).

    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?
    --
    "No one has the right to destroy another person's belief by
    demanding empirical evidence." -- Ann Landers
     
    Walter Roberson, Feb 1, 2007
    #4
  5. Umesh

    Umesh Guest

    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++).
    >
    > 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?
    > --
    > "No one has the right to destroy another person's belief by
    > demanding empirical evidence." -- Ann Landers
     
    Umesh, Feb 1, 2007
    #5
  6. Umesh

    Dave Steffen Guest

    "Umesh" <> writes:

    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.

    ----------------------------------------------------------------------
    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
    #6
  7. Umesh

    Mark P Guest

    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
    #7
  8. Umesh

    Umesh Guest

    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:
    >
    > 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.
    >
    > ----------------------------------------------------------------------
    > 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
    #8
  9. Umesh

    Andre Kostur Guest

    "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


    Short answer:

    Do it yourself. If you get stuck, ask a specific question.
     
    Andre Kostur, Feb 1, 2007
    #9
  10. Umesh

    Ian Collins Guest

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


    --
    Ian Collins.
     
    Ian Collins, Feb 1, 2007
    #10
  11. 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?


    >> A question about your specifications: should upper and lower


    >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

    --
    All is vanity. -- Ecclesiastes
     
    Walter Roberson, Feb 1, 2007
    #11
  12. 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
    #12
  13. Umesh

    Default User Guest

    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.



    Please don't top-post. Your replies belong following or interspersed
    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
    #13
  14. >>>>> "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
    #14
  15. On Feb 1, 1:27 pm, "Umesh" <> wrote:
    > 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.
     
    Markus Svilans, Feb 1, 2007
    #15
  16. 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
    #16
  17. Umesh

    CBFalconer Guest

    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
    #17
  18. Umesh

    CBFalconer Guest

    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?


    The answer is still '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
  19. Umesh

    CBFalconer Guest

    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.


    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)
     
    CBFalconer, Feb 1, 2007
    #19
  20. Umesh

    Mark P Guest

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

    > 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
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stig Christiansen

    Tomcat, mod_jk, percentage character in URI?

    Stig Christiansen, Sep 8, 2004, in forum: Java
    Replies:
    0
    Views:
    440
    Stig Christiansen
    Sep 8, 2004
  2. Mihai

    Percentage matching of text

    Mihai, Oct 1, 2004, in forum: Java
    Replies:
    1
    Views:
    739
    Michael Borgwardt
    Oct 1, 2004
  3. Bruce Eckel

    Percentage matching of text

    Bruce Eckel, Jul 30, 2004, in forum: Python
    Replies:
    8
    Views:
    435
    Steve Christensen
    Aug 9, 2004
  4. Umesh
    Replies:
    44
    Views:
    1,180
    Richard Bos
    Feb 5, 2007
  5. D
    Replies:
    0
    Views:
    230
Loading...

Share This Page