How can I use pointers in C to display how many occurrences each alphabetic letter appears in an array?

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

Question

I am creating a small ANSI C application using GCC in Ubuntu. I currently have two C source files and one header file already created (I'm required to do so by my lab professor).

My "main" C file:

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

int main(void) {
    int ABStats[26] = { 0 };
    int *pABStats = ABStats;
    char *pAr = Ar;

    GetFrequency(pAr, pABStats);

    return EXIT_SUCCESS;
}

My alphaStats.h header file:

#include "alphaStats.c"

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);

My alphaStats.c source file:

int GetFrequency(char *pAr, int *pABStats) {
    int counter, chNum = 0, i;
    char ch = 'A';

    for (*pAr = pABStats; *pAr != '\0'; *pAr++) {
        chNum = isalpha(*pAr) ? (toascii(toupper(*pAr)) - ch++) : -1;
        if (*pAr == chNum)
            counter++;
    }
    printf("%d", chNum);
}

void DisplayVHist(int *pABStats, int size) {

}

My goal is to use C pointers (*pAr and *pABStats) to iterate through the char array (which is named Ar) and print how often the letters A-Z appear in the char array Ar. At this point I am having issues with coding the function GetFrequency(). I understand how to use a for loop with an index to find and print contents, but I am new to C programming and I am having trouble with using pointers.

Any help would be great. Thank you in advance.

No correct solution

OTHER TIPS

One thing to note is, In main function, char *pAr = &Ar; & int *pABStats = &ABStats; is not what you want. Since Ar is array of characters and it is base address which need to be assigned to pointer pAr, i.e char *pAr = Ar

int GetFrequency(char *pAr, int *pABStats) {
    int chNum = 0;

    for (; *pAr != '\0'; ++pAr) {
        if(isalpha(*pAr)){
            chNum = toupper(*pAr) - 'A';
            ++pABStats[chNum];
        }
    }
}

int main(void) {
    int ABStats[26] = { 0 };

    GetFrequency(Ar, ABStats);

    return EXIT_SUCCESS;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top