首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用C显示数组中每个字母字符出现的次数?

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

Stack Overflow用户
提问于 2014-02-22 19:12:07
回答 1查看 121关注 0票数 0

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

我的主要档案:

代码语言:javascript
复制
#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;

}

字母Stats.h文件:

代码语言:javascript
复制
#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);

字母Stats.c文件:

代码语言:javascript
复制
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++);
    }
}

我需要使用指针来计算Ar[]中每个字符发生了多少次(即使用pAr和pABStats)。当我运行上述代码时,会出现分段错误。

有谁知道为什么我的代码不能工作,并可能帮助我完成它的编码吗?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-22 19:21:23

你错过了一个*

代码语言:javascript
复制
for (; pAr != '\0'; pAr++) {
//    ^ need a *

其他评论:

#include通常应该在.c文件中,除非在头文件中需要来自它们的类型或其他东西。此外,

代码语言:javascript
复制
#include "alphaStats.c"

永远不要包含.c文件。要么立即编译所有内容(gcc foo.c bar.c -o program),要么(更频繁地)使用对象文件(gcc -c foo.c; gcc -c bar.c; gcc foo.o bar.o -o program)。所有这些gcc都应该启用了警告(-Wall -Wextra -pedantic),只是在示例中忽略它们就更清楚了。

下一首,

代码语言:javascript
复制
char Ar[] = {"..."};

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

另外,如果您需要在另一个文件中使用,那么应该在其中一个C文件中,将extern char Ar[]放在头文件中。

最后,您应该花时间研究如何使用gdb

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21959123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档