gpt4 book ai didi

c - 我的小 C 程序崩溃了

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

这是一个程序,它应该在一个字符串中找到最常见的元素。但是当我输入一个字符串时它崩溃了。

#include <stdio.h>
#include <conio.h>
#include <string.h>
int main(){
char a[100];
scanf("%s", a);
int max=0,n,k;
int urt = strlen(a);
for(int i=0; i<urt-1; i++){
n=0;
for(int l=i+1; l<urt; l++){
if(a[i]==a[l]) n++;
}
if(max<n){
max=n;
k=i;
}
}
printf("%s\n", a[k]);
printf("%d", max);
getch();
return 0;
}

最佳答案

error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’

这意味着你需要改变:

printf("%s\n", a[k]);

到:

printf("%c\n", a[k]);

因为a[k]不是字符串,而是字符。此外,为了获得更准确的输出,将 1 添加到 n 以考虑您正在扫描的字符(从外循环):

n=1; // instead of n=0;

关于c - 我的小 C 程序崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13560785/

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