Thread: letter frequency

  1. #1
    Registered User knoxville's Avatar
    Join Date
    Mar 2005
    Location
    istanbul
    Posts
    13

    letter frequency

    in a part of my project i need to print letter frequencies to the screen. it's some kind of deciphering, changing letters according to the frequency. but i couldnt find a way to print them.

    ty for your help

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    So you know how to count them but not print them? Have you tried printf()?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User knoxville's Avatar
    Join Date
    Mar 2005
    Location
    istanbul
    Posts
    13
    my bad, the problem is counting

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Try:
    Code:
    char str[] = "ioawjoiajoiwehfaiohfiasjdfaoijfoiawhuahgowienvawecm";
    int count[26] = { 0 };
    int i;
    
    for(i = 0;str[i];++i)
      count[str[i] - '0']++;
    Then count[0] represents how many a's there are, count[1] is how many b's, and so on. You'll have to change it a bit to handle upper and lower case, punctuation, etc...whatever might be in the string.

    EDIT: Another possibility is to just make the count[] array 128 elements big (256 if str holds unsigned chars) and then just do:
    Code:
    count[str[i]]++;
    Last edited by itsme86; 07-31-2006 at 11:27 AM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    huffman encoding per chance?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    str[i] - '0'
    should be
    Code:
    str[i] - 'a'
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by XSquared
    Code:
    str[i] - '0'
    should be
    Code:
    str[i] - 'a'
    Oops...thanks for catching that.
    If you understand what you're doing, you're not learning anything.

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    No problem. I don't provide any useful input, I'm just a pedantic ass.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Your compiler has never failed me.
    $ xsquared foo.c -o foo -Wall -ansi -pedantic-ass

    Except now that I think of it, it would really help if you made a version where the errors weren't displayed in the form of a short riddle.
    Last edited by whiteflags; 07-31-2006 at 11:43 AM.

  10. #10
    Registered User knoxville's Avatar
    Join Date
    Mar 2005
    Location
    istanbul
    Posts
    13
    thanks for help. now i can continue on my project but,
    i need to use clrscr(); can i just copy paste conio.h into dev-c++ library files?

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  12. #12
    Registered User knoxville's Avatar
    Join Date
    Mar 2005
    Location
    istanbul
    Posts
    13
    aight everything works fine. but, every time i change letters frequency numbers doubles. example output:

    blablasometext
    ---------------

    b=2
    l=2...

    change: b
    new: a

    -clrscr-

    blablasometext
    a--a----------

    b=4
    l=4..


    Code:
    else{
                   i=0;
                   for (i=0;n_msg2[i]!='\0';i++){
                       if(n_msg2[i]=='\n'){
                                           n_msg2[i]='\n';}
                       else{
                            n_msg2[i]='-';}}
                                                                   
                   do{
                   clrscr();
                   printf("\n\n%s",msg2);
                   printf("\n\n%s",n_msg2);
                   printf("\n\n\n\nLETTER FREQUENCY\n");
                   
                   for (l_freq=0;msg2[l_freq];++l_freq){
                       freq[msg2[l_freq]-'a']++;
                       }
                  
                   letter='a';
                   do{
                      printf("%c - %d\n",letter,freq[l_counter]);
                      l_counter++;
                      letter++;   }while (l_counter!=26);
                   
                   printf("\nEnter the letter to change - 0 to Quit: ");
                   change=getch();
                   printf("\nEnter the new letter - 0 to Quit: ");
                   n_letter=getch();
    
                   i=0;
                   do{
                   if(msg2[i]!=change){
                        i++;          }
                   else{
                        n_msg2[i]=n_letter;
                        i++;
                        }                  }while(msg2[i]!='\0');
                   }while(change!='0'); }}

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Doesn't look like you're setting the frequency count back to 0 between loop iterations...
    If you understand what you're doing, you're not learning anything.

  14. #14
    Registered User knoxville's Avatar
    Join Date
    Mar 2005
    Location
    istanbul
    Posts
    13
    i tried..

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Next time, try posting your code.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. counting letter occurences in a string
    By pjr5043 in forum C++ Programming
    Replies: 35
    Last Post: 05-05-2008, 09:18 PM
  2. Advice requested, Code makes sense to me, not compiler
    By andrew.bolster in forum C Programming
    Replies: 53
    Last Post: 01-06-2008, 01:44 PM
  3. help using strings and mapping
    By trprince in forum C Programming
    Replies: 29
    Last Post: 12-01-2007, 04:01 PM
  4. Big Letter became small letter
    By cogeek in forum C Programming
    Replies: 27
    Last Post: 12-13-2004, 02:04 PM
  5. Replies: 5
    Last Post: 11-20-2003, 01:27 AM