gpt4 book ai didi

使用 C 中的 switch 语句计算字符串中出现的空格、逗号和点

转载 作者:行者123 更新时间:2023-11-30 16:25:52 28 4
gpt4 key购买 nike

#include <stdioh.>
#include <string.h>
int main()
{
char a[100];
int i,contor=0;
printf("Introduceti sirul: ");
gets(a);
switch(a)
{
case ',':
contor++;
break;
case '.':
contor++;
break;
case ' ':
contor++;
break;
default:
printf("Nu exista spatii,virgule sau puncte.");
}
printf("Numarul de spatii, virgule si puncte este: %d",contor);
return 0;
}

我在这里尝试过,但它给了我一个错误 - 开关数量不是整数
有人帮忙吗?:))我无法解决这个问题

最佳答案

switch(a) 不起作用,因为 a 是一个数组(衰减为指向 char 的指针)。您可能想要类似的东西:

    char *cp= a;
while (*cp)
{
switch(*cp)
{
case ',':
contor++;
break;
// etc
}
cp++;
}

关于使用 C 中的 switch 语句计算字符串中出现的空格、逗号和点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53239570/

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