Character Frequency in an Array of String Pointers

I am working on an exercise that asks me:

  • read 20 words from the user,
  • save them in an array of pointers depending on their size with memory allocation.
  • find the frequency of the letters
  • print them on a histogram.

Step 1 and 2 work fine in my code. The problem is the frequency. I posted a test printfto see if it works, and it turns out that it does not take characters into account.

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

#define N         20
#define MAX_SIZE  200

int main() {
    char *words[N];
    int i, c = 0, length;
    char *temp;
    int freq[26] = { 0 };

    temp = (char *)malloc(MAX_SIZE * sizeof(char));

    for (i = 0; i < N; i++) {
        printf("Give a word:");
        gets(temp);
        length = strlen(temp);
        *(words + i) = (char *)malloc((length + 1) * sizeof(char));
        strcpy(*(words + i), temp);
        printf("%s\n", *(words + i));
    }
    free(temp);

    while (*words[c] != '\0' && c < 20) {
        if ((*words[c] >= 'a' && *words[c] <= 'z') || (*words[c] >= 'A' &&  *words[c] <= 'Z')) {                    
            freq[*words[c] - 'a']++;
            words[c]++;
        }
        c++;
    }

    for (i = 0; i < 26; i++) {
        printf("%c occurs %d times in the entered string.\n", i + 'a', freq[c]);
    }
    return 0;
}
+4
source share
3 answers

you seem to check only the first letter of each word, then move on to the next word. obviously you need 2 loops working there.

or something like:

while (c<N) {
    if(( *words[c]>='a' && *words[c]<='z') || (*words[c]>='A' &&  *words[c]  <='Z')) {                    

        freq[*words[c]-'a']++;
    }
    words[c]++;
    if(*words[c] == '\0')
        c++;
}

, , , .

+1

:

  • words .
  • , freq[*words[c]-'a'].
  • : freq[i] freq[i]. freq[c].
  • temp.
  • gets, . gets , .
  • malloc

:

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

#define N         20
#define MAX_SIZE  200

int main(void) {
    char *words[N];
    int n, i, j, c, length;
    char temp[MAX_SIZE];
    int freq[26] = { 0 };

    for (n = 0; n < N; n++) {
        printf("Give a word:");
        if (!fgets(temp, sizeof temp, stdin))
            break;
        temp[strcspn(temp, "\n")] = '\0';  /* strip the \n if present */
        length = strlen(temp);
        words[n] = malloc(length + 1);
        if (words[n] == NULL) {
            printf("cannot allocate memory\n");
            exit(EXIT_FAILURE);
        }
        strcpy(words[n], temp);
        printf("%s\n", words[n]);
    }

    for (i = 0; i < n; i++) {
        for (j = 0; (c = words[i][j]) != '\0'; j++) {
            if (c >= 'a' && c <= 'z')
                freq[c - 'a']++;
            else
            if (c >= 'A' && c <= 'Z')
                freq[c - 'A']++;
        }
    }

    for (i = 0; i < 26; i++) {
        if (freq[i] > 0)
            printf("%c occurs %d times in the entered strings.\n", i + 'a', freq[c]);
    }
    return 0;
}
+2

. , .

    if (*words[c]>='a' && *words[c]<='z'){                       
        freq[*words[c]-'a']++;
        words[c]++;
    }
    else if (*words[c]>='A' &&  *words[c]  <='Z'){                       
        freq[*words[c]-'A']++;
        words[c]++;
    }
0

Source: https://habr.com/ru/post/1624953/


All Articles