在C語言中,可以通過以下方法統(tǒng)計字符串的個數(shù):
下面是使用循環(huán)遍歷字符串的示例代碼:
#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("字符串個數(shù):%d\n", count);
return 0;
}
在上面的示例代碼中,通過循環(huán)遍歷字符串,統(tǒng)計字符串中非空字符的個數(shù)??崭褡址话ㄔ诮y(tǒng)計中。