如何使用C显示数组中每个字母字符的出现次数?

如何使用C显示数组中每个字母字符的出现次数?,c,arrays,pointers,ansi,C,Arrays,Pointers,Ansi,我试图用ANSI C编写一个小应用程序。该应用程序应该使用指针打印出字符数组中每个字母的出现次数 我的主文件: #include "alphaStats.h" int main(void) { int ABStats[26] = { 0 }; char *pAr = Ar; int *pABStats = ABStats; GetFrequency(pAr,pABStats); DisplayVHist(pABStats,ALPHABET_SIZE

我试图用ANSI C编写一个小应用程序。该应用程序应该使用指针打印出字符数组中每个字母的出现次数

我的主文件:

#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文件:

#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”
#定义字母表大小26
字符Ar[]={"所有高卢人分为三个部分,一个是居住在Belgae的人,另一个是居住在Aquitani的人,那些用自己的语言被称为凯尔特人的人,在我们的高卢人中,第三个是被称为凯尔特人的人。所有这些在语言、习俗和法律上都是不同的。加隆河将高卢人与Aquitani分开;马恩河和塞纳河将高卢人与Belgae分开这些,贝尔盖人是最勇敢的,因为他们离[我们的]文明和文明最远他们与居住在莱茵河外的德国人最为接近,他们不断地与德国人进行战争;因此,赫尔维提亚人在英勇方面也超过了其他高卢人,因为他们在近十年内与德国人展开了斗争日常战争,当他们将他们从自己的领土上驱逐出去,或者自己在他们的边界上发动战争时。其中一部分,据说是高卢人占领的,开始于罗纳河;它以加隆河、海洋和贝尔盖的领土为界;它也与塞瓜尼河和塞瓜尼河接壤海尔维蒂号,位于莱茵河畔,向北延伸。摘自《凯撒征服高卢》,译者:W.A.麦克德维特。译者:W.s.博恩。第一版。纽约。哈珀兄弟出版社。1869年。哈珀的新古典图书馆。发表于《知识共享》下,可在http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:1999.02.0001"};
int-GetFrequency(char*,int*);
无效显示列表(int*,int);
alphaStats.c文件:

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++);
    }
}
int-GetFrequency(字符*pAr,int*pABStats){
对于(;PAR=‘0’;PAR++){
字符c=*pAr;
如果(!isalpha(c))继续;
pABStats[(int)(toupper(c)-'A')]++;
}
返回0;
}
无效显示列表(int*pABStats,int size){
int i;
对于(i=0;i

我需要使用指针来确定Ar[]中每个字符的出现次数(即使用PAR和PABSTATS)。当我运行上面的代码时,我得到一个分割错误。


有人知道为什么我的代码不能正常工作吗?有人能帮我完成代码编写吗?谢谢。

您错过了一个
*

for (; pAr != '\0'; pAr++) {
//    ^ need a *
其他意见:

#include
s通常应位于
.c
文件中,除非您需要在标题中使用类型或其他内容。此外

#include "alphaStats.c"
不要包含.c文件。要么立即编译所有文件(
gcc-foo.c-bar.c-o程序
),要么(更频繁地)使用对象文件(
gcc-c-foo.c;gcc-c-bar.c;gcc-foo.o-o程序
)。所有这些
gcc
都应该启用警告(
-Wall-Wextra-pedantic
),在示例中省略它们就更清楚了

其次,

不知道为什么会编译,但你应该去掉大括号。字符串已经是
char[]

此外,如果需要,应该在其中一个C文件中,将它放在另一个C文件中,将
extern char Ar[]
放在头中

最后,花时间研究如何使用
gdb
是值得的

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