在C语言中,可以通过以下方法统计字符串的个数:
下面是使用循环遍历字符串的示例代码:
#include <stdio.h>
int countStrings(char* str) {
int count = 0;
int i = 0;
while (str[i] != '\0') {
if (str[i] != ' ')
count++;
i++;
}
return count;
}
int main() {
char str[100];
printf("请输入字符串:");
fgets(str, sizeof(str), stdin);
int count = countStrings(str);
printf("字符串个数:%d\n", count);
return 0;
}
在上面的示例代码中,通过循环遍历字符串,统计字符串中非空字符的个数。空格字符不包括在统计中。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:c语言怎么统计字符串个数