How can I display the number of occurrences of each alphabetic character in an array using C?

StackOverflow https://stackoverflow.com/questions/21959123

  •  15-10-2022
  •  | 
  •  

Question

I am attempting to code a small application using ANSI C. The application is supposed to print out the number of occurrences of each letter in a char array using pointers.

My main file:

#include "alphaStats.h"

int main(void) {

    int ABStats[26] = { 0 };

    char *pAr = Ar;
    int *pABStats = ABStats;

    GetFrequency(pAr,pABStats);
    DisplayVHist(pABStats,ALPHABET_SIZE);

    return EXIT_SUCCESS;

}

alphaStats.h file:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "alphaStats.c"

#define ALPHABET_SIZE 26

char Ar[] = {"All Gaul is divided into three parts, one of which the Belgae inhabit, the Aquitani another, those who in their own language are called Celts, in our Gauls, the third. All these differ from each other in language, customs and laws. The river Garonne separates the Gauls from the Aquitani; the Marne and the Seine separate them from the Belgae. Of all these, the Belgae are the bravest, because they are furthest from the civilization and refinement of [our] Province, and merchants least frequently resort to them, and import those things which tend to effeminate the mind; and they are the nearest to the Germans, who dwell beyond the Rhine , with whom they are continually waging war; for which reason the Helvetii also surpass the rest of the Gauls in valor, as they contend with the Germans in almost daily battles, when they either repel them from their own territories, or themselves wage war on their frontiers. One part of these, which it has been said that the Gauls occupy, takes its beginning at the river Rhone ; it is bounded by the river Garonne, the ocean, and the territories of the Belgae; it borders, too, on the side of the Sequani and the Helvetii, upon the river Rhine , and stretches toward the north. From 'Caesar's Conquest of Gaul', Translator. W. A. McDevitte. Translator. W. S. Bohn. 1st Edition. New York. Harper & Brothers. 1869. Harper's New Classical Library. Published under creative commons and available at http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:1999.02.0001"};

int GetFrequency(char*,int*);
void DisplayVHist(int*,int);

alphaStats.c file:

int GetFrequency(char *pAr, int *pABStats) {
    for (; pAr != '\0'; pAr++) {
        char c = *pAr;
        if (!isalpha(c)) continue;

        pABStats[(int)(toupper(c) - 'A')]++;
    }
    return 0;
}

void DisplayVHist(int *pABStats, int size) {
    int i;
    for (i = 0; i < size; i++) {
        printf("'%c' has %2d occurrences.\n", i + 'a', *pABStats++);
    }
}

I am required to use pointers to figure out how many occurrences of each character in Ar[] occur (i.e. using pAr and pABStats). I get a segmentation fault when I run the above code.

Does anybody know why my code is not functioning, and could possibly help me finish coding it? Thanks.

Was it helpful?

Solution

You missed a *.

for (; pAr != '\0'; pAr++) {
//    ^ need a *

Other comments:

#includes should generally be in the .c file, unless you need a type or something from them in the header. Furthermore,

#include "alphaStats.c"

never include a .c file. Either compile everything at once (gcc foo.c bar.c -o program) or (more frequently) use object files (gcc -c foo.c; gcc -c bar.c; gcc foo.o bar.o -o program). And all those gccs should have warnings enabled (-Wall -Wextra -pedantic), it's just more clear to omit them in the examples.

Next,

char Ar[] = {"..."};

Not sure why that even compiles, but you should drop the braces. A string is already a char[].
Also, that should be in one of the C files, if you need it in the other put extern char Ar[] in the header.

And finally, it would be worth your time to figure out how to use gdb.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top