Sean
Sean

Reputation: 3450

Program that reads three lines of text and counts each alphabet letter (C)

I'm fairly new to coding and am currently enrolled in a course at school teaching C. We have been given an assignment that requires as follows:

Write a program that inputs three lines of text and uses the function strchr to determine the number of occurrences of each letter of the alphabet (uppercase and lowercase should be counted as the same). Store the totals for each letter in an array and print the result.

Here's the code I've written so far:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 100

int main(void) {
    int alphabet[26] = { 0 };
    char string[3][SIZE];
    int i, j;
    int c;

    printf("Enter three lines of text:\n");

    for (i = 0; i <= 2; i++) {
        fgets(string[i], SIZE, stdin);
    }

    for (i = 0; i <= 2; i++) {
        for (j = 0; &string[i][j] != '\0'; j++) {
            string[i][j] = tolower(string[i][j]);
        }
        for (j = 0; &string[i][j] != '\0'; j++) {
            if (strchr(&string[i][j], ('a' + j)) != NULL) {
                alphabet[j]++;
            }
        }       
    }
    printf("\n");

    for (i = 0; i < 26; i++) {
        printf("%c: %d\n", ('a' + i), alphabet[i]);
    }
    printf("\n");

    return 0;
}

The problem with the current program I have is that the counter for the array alphabet[] doesn't seem to be incrementing the count properly.

The output should count, for example, increment by 1 for each occurrence of every letter, but the results are all 0.

Any other tips or words of advice are greatly appreciated!

Upvotes: 0

Views: 1190

Answers (2)

Pras
Pras

Reputation: 4044

You need to change loop terminating condition from &string[i][j] != '\0'; to string[i][j] != '\0'; strchr will return the address from the string where match was found, so you should save the non null address to pass it to next call to strchr Something like this:

tmpPtr = &string[i][j];
while ((tmpPtr=strchr(tmpPtr, string[i][j])) != NULL) {
alphabet[string[i][j] - 'a']++;
}

Upvotes: 0

&#212;rel
&#212;rel

Reputation: 7632

Your test for end of line is wrong, no need to loop several time just parse the string and count the letters You want letter between 'a'and 'z', check if the char is in the range and count it

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define SIZE 100

int main(void) {
    int alphabet[26] = { 0 };
    char string[3][SIZE];
    int i, j;

    printf("Enter three lines of text:\n");

    for (i = 0; i <= 2; i++) {
        fgets(string[i], SIZE, stdin);
    }

    for (i = 0; i <= 2; i++) {
        for (j = 0; string[i][j] != '\0'; j++) {
            char c = tolower(string[i][j]);
            if (c >= 'a' && c <= 'z') {
                alphabet[c - 'a']++;
            }
        }
    }
    printf("\n");

    for (i = 0; i < 26; i++) {
        printf("%c: %d\n", ('a' + i), alphabet[i]);
    }
    printf("\n");

    return 0;
}

Upvotes: 1

Related Questions