Thread: In over my head with character frequency code

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    1

    In over my head with character frequency code

    Hi everyone,

    I have decided to take a intermediate level C programming course with absolutely no programming experience. Most of the HW involved manipulating integers but the last question involves strings and I don't know how to manipulate them and I am running out of time.

    I have to create a program that features a loop that prompts the user to enter a string of lower case letters and tallies the frequency of each character. The code I have does that, but I can't figure out how to phrase the loop condition to that will prompt the user again for a string and then add those character tallies to the total. The program is supposed to keep prompting and adding until an EOF character (CTRL+Z or ^Z) is entered.

    All my attempts either go into an infinite loop without printing or overwrites previous counts a new tally for every subsequent string.

    HELP!

    Code:
    #include<stdio.h>
    #include<string.h>
     
    main()
    {
       char string[100], ch;
       int c = 0, count[26] = {0};
     
       printf("Enter a string");
       scanf("%c", &string);
     
       while ( string[c] != ''\0' )
       {
     
          if ( string[c] >= 'a' && string[c] <= 'z' ) 
    
             count[string[c]-'a']++;
    
          c++;
       }
     
       for ( c = 0 ; c < 26 ; c++ )
       {
          if( count[c] != 0 )
             printf("%c occurs %d times in the entered string.", c+'a', count[c]);
    
       }
     
       return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First in your scanf() %c is used to retrieve a single character not a "string", you may want to read the documentation for scanf() and use the proper specifier for a string.

    Jim

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Line 12 can't​ be right...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count and print the frequency of each ASCII character
    By s_jsstevens in forum C Programming
    Replies: 3
    Last Post: 02-07-2011, 09:33 PM
  2. Character frequency counting
    By zcrself in forum C Programming
    Replies: 2
    Last Post: 03-01-2010, 11:04 AM
  3. frequency character counter
    By hasanah in forum C Programming
    Replies: 4
    Last Post: 04-15-2009, 01:28 AM
  4. Character frequency table
    By raidkridley in forum C Programming
    Replies: 1
    Last Post: 02-12-2009, 06:31 AM
  5. about program that counts a character frequency
    By Unregistered in forum C++ Programming
    Replies: 15
    Last Post: 04-23-2002, 01:21 PM