C 字符串指针数组中字符的频率
我正在进行一项练习,要求我:C 字符串指针数组中字符的频率,c,string,pointers,C,String,Pointers,我正在进行一项练习,要求我: 从用户那里读20个单词 根据指针的大小和内存,将其保存在指针数组中, 分配 找出字母的频率 在柱状图上打印出来 第1步和第2步在我的代码上运行良好。问题出在频率上。我放置了一个测试printf,看看它是否工作,结果显示它不计算字符数 #include <stdio.h> #include <stdlib.h> #include <string.h> #define N 20 #define MAX_SIZE 2
printf
,看看它是否工作,结果显示它不计算字符数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 20
#define MAX_SIZE 200
int main() {
char *words[N];
int i, c = 0, length;
char *temp;
int freq[26] = { 0 };
temp = (char *)malloc(MAX_SIZE * sizeof(char));
for (i = 0; i < N; i++) {
printf("Give a word:");
gets(temp);
length = strlen(temp);
*(words + i) = (char *)malloc((length + 1) * sizeof(char));
strcpy(*(words + i), temp);
printf("%s\n", *(words + i));
}
free(temp);
while (*words[c] != '\0' && c < 20) {
if ((*words[c] >= 'a' && *words[c] <= 'z') || (*words[c] >= 'A' && *words[c] <= 'Z')) {
freq[*words[c] - 'a']++;
words[c]++;
}
c++;
}
for (i = 0; i < 26; i++) {
printf("%c occurs %d times in the entered string.\n", i + 'a', freq[c]);
}
return 0;
}
#包括
#包括
#包括
#定义n20
#定义最大尺寸200
int main(){
字符*字[N];
int i,c=0,长度;
字符*温度;
int freq[26]={0};
temp=(char*)malloc(MAX_SIZE*sizeof(char));
对于(i=0;i 如果((*words[c]>='a'&&&rds[c]='a'&&rds[c]从区分大写和小写开始。这可能不是完整的解决方案,但这是一个开始
if (*words[c]>='a' && *words[c]<='z'){
freq[*words[c]-'a']++;
words[c]++;
}
else if (*words[c]>='A' && *words[c] <='Z'){
freq[*words[c]-'A']++;
words[c]++;
}
如果(*words[c]>='a'&&&&rds[c]='a'&&rds[c]您的代码有几个问题:
- 您可以修改
words
数组中的指针,而不是通过索引进行迭代
- 在本例中,您可以使用
freq[*words[c]-'a']
检查大写字母,但访问其边界以外的偏移量
- 最后一个循环中有一个输入错误:如果
freq[c]
,则应该是freq[i]
- 您不需要分配阵列
temp
- 您不能使用已从标准中删除的
gets
。gets
无法检查缓冲区溢出,任何恶意输入都可能造成无法估量的后果
- 您应该检查
malloc
故障
以下是更正的版本:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 20
#define MAX_SIZE 200
int main(void) {
char *words[N];
int n, i, j, c, length;
char temp[MAX_SIZE];
int freq[26] = { 0 };
for (n = 0; n < N; n++) {
printf("Give a word:");
if (!fgets(temp, sizeof temp, stdin))
break;
temp[strcspn(temp, "\n")] = '\0'; /* strip the \n if present */
length = strlen(temp);
words[n] = malloc(length + 1);
if (words[n] == NULL) {
printf("cannot allocate memory\n");
exit(EXIT_FAILURE);
}
strcpy(words[n], temp);
printf("%s\n", words[n]);
}
for (i = 0; i < n; i++) {
for (j = 0; (c = words[i][j]) != '\0'; j++) {
if (c >= 'a' && c <= 'z')
freq[c - 'a']++;
else
if (c >= 'A' && c <= 'Z')
freq[c - 'A']++;
}
}
for (i = 0; i < 26; i++) {
if (freq[i] > 0)
printf("%c occurs %d times in the entered strings.\n", i + 'a', freq[c]);
}
return 0;
}
#包括
#包括
#包括
#定义n20
#定义最大尺寸200
内部主(空){
字符*字[N];
int n,i,j,c,长度;
字符温度[最大大小];
int freq[26]={0};
对于(n=0;n='a'&&c='a'&&c 0)
printf(“%c在输入的字符串中出现%d次。\n”,i+'a',freq[c]);
}
返回0;
}
您似乎只检查每个单词的第一个字母,然后继续检查下一个单词。显然,您需要两个循环
或者类似于:
while (c<N) {
if(( *words[c]>='a' && *words[c]<='z') || (*words[c]>='A' && *words[c] <='Z')) {
freq[*words[c]-'a']++;
}
words[c]++;
if(*words[c] == '\0')
c++;
}
while(c='a'&&&&*words[c]='a'&&&*words[c]缩进您的代码将有助于其他人阅读代码并理解您的问题所在。此外,当您增加频率时,您不会区分大小写。“b'-'a'与“b'-'a”不同,并且会给您带来问题您知道*(words+i)
相当于单词[i]
?虽然两者可以互换使用,但后者通常是首选的,因为它真正显示了您正在做什么(为数组编制索引)。此外,sizeof(char)
根据C规范,总是会产生值1
。@BlueMoon93你是对的,我会处理它,你能解释更多你的2st注释吗?@JoachimPileborg是的,我知道这是相同的理解,我立即就做。不包括并使用islower(),isupper()更简单?@BlueMoon93我做到了,现在有时它会告诉我所有字母都显示了1次,而其他时候它会告诉我所有字母都显示了0。@user2719403检查chqrlie的答案。他的答案解决了这个问题bug@BlueMoon93部分解决了问题哦,天哪!你是对的。它解决了部分问题,例如,如果我输入;如果我输入'ab',它将只y数“a”而不是“b”。我找到了解决方案,我认为..如果您在上面的代码中更改!=with==它会工作..因为当没有更多字符可读时,它将转到下一个单词..哦,很抱歉给出了一个错误的解决方案^^。很高兴它有帮助。没问题,谢谢