Character Frequency Tables from array Etc.

This is a discussion on Character Frequency Tables from array Etc. within the C Programming forums, part of the General Programming Boards category; Hello everyone, I am having two problems in my code, I believe they might be tied together in my the ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    4

    Character Frequency Tables from array Etc.

    Hello everyone, I am having two problems in my code, I believe they might be tied together in my the same incorrect line of thinking. So I'm hoping for a 3rd party perpective.

    Problems are as follows:
    Code:
    printf("String 3 is: %s\n\n", SplitString);
    is cutting off the last letter.

    AND

    -I can't get the frequencies to calculate properly, maybe because it's missing counting a letter.

    Thank you

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /**65-90 A-Z 97 **/
    int main() 
    {
        char InString1[255] = {0};
        char InString2[255] = {0};
        char InString3[101] = {0};
        char SplitString[255];
        int LengthInString1;
        int LengthInString2;
        int LengthInString3;
        int LengthSplitString;
        int Frequency[128] = {0};
        int c = 0;
        
        printf("Enter string 1: ");
        fgets(InString1,254,stdin);
        printf("Enter string 2: ");
        fgets(InString2,254,stdin);
        printf("Enter string 3: ");
        fgets(InString3,101,stdin);
        
        LengthInString1 = strlen(InString1) - 1; 
        LengthInString2 = strlen(InString2) - 1;
        LengthInString3 = strlen(InString3) - 1;
        
        InString1[LengthInString1 - 1] = '\0';
        InString2[LengthInString2 - 1] = '\0';
        InString3[LengthInString3 - 1] = '\0';
        
        strncpy(SplitString, InString1, LengthInString1 / 2);
        strcat (SplitString, &InString2[(LengthInString2 / 2)]);
        
        LengthSplitString = strlen(SplitString) - 1;
        SplitString[LengthSplitString + 1] = '\0';
    
        
        printf("String 1 is %d bytes long, and String 2 is %d bytes long\n", LengthInString1 , LengthInString2);
    
            printf("String 3 is: %s\n\n", SplitString);
        
        printf("FREQUENCY TABLE\n");
        printf("--------------------------------\n");
        printf("Char\tCount\t%% of Total\n");
        printf("----\t-----\t----------------\n");
        
        while ( InString1[c] != '\0' )
        {
            if ( InString1[c] >= 'a' && InString1[c] <= 'z' )
                Frequency[InString1[c]-'a']++;
            
            else if ( InString1[c] >= 'A' && InString1[c] <= 'Z' )
                Frequency[InString1[c]-'A']++;
            c++;
        }
        
        for ( c = 0 ; c < 52 ; c++ )
        {
            if( Frequency[c] != 0 )
            {
                printf("%c\t%d\t%.2f\n",c+'a',Frequency[c],(float)Frequency[c]/c*100);
            }
        }
        system("pause");
        return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,655
    strlen is a function from string.h that returns the number of letters of the word (without counting the null terminator).
    You set the length equal to srtlen(..)-1 .

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    THANKS. Yes I toyed with those to the point that it was quite ugly, but to get back on the right track I adjusted all as suggested and sure enough fixed my first problem and the fact that I was missing a count. unfortuanately, the percentages are still off though. i'll keep messing with it...

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    got it, shouldn't have been using "c" as a valid total as it was changing. added another counter and i got it! thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head with character frequency code
    By steadyhanded in forum C Programming
    Replies: 2
    Last Post: 02-08-2012, 04:06 PM
  2. Character frequency counting
    By zcrself in forum C Programming
    Replies: 2
    Last Post: 03-01-2010, 10: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, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21