gpt4 book ai didi

c - 如何使用 C 中的指针来显示每个字母在数组中出现的次数?

转载 作者:太空宇宙 更新时间:2023-11-04 08:46:24 25 4
gpt4 key购买 nike

我正在 Ubuntu 中使用 GCC 创建一个小型 ANSI C 应用程序。我目前已经创建了两个 C 源文件和一个头文件(我的实验室教授要求我这样做)。

我的“主”C 文件:

#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;
}

我的 alphaStats.h 头文件:

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

我的 alphaStats.c 源文件:

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

}

我的目标是使用 C 指针(*pAr 和 *pABStats)遍历 char 数组(名为 Ar)并打印字母 A-Z 在 char 数组 Ar 中出现的频率。在这一点上,我在编写函数 GetFrequency() 时遇到了问题。我了解如何使用带索引的 for 循环来查找和打印内容,但我是 C 编程新手,在使用指针时遇到问题。

任何帮助都会很棒。提前谢谢你。

最佳答案

需要注意的是,在main函数中,char *pAr = &Ar; & int *pABStats = &ABStats;不是什么你要。由于Ar是字符数组,是指针pAr需要赋值的基地址,即char *pAr = Ar

关于c - 如何使用 C 中的指针来显示每个字母在数组中出现的次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21535312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com