统计字符串的个数
#include <stdio.h>
int countCharacters(char* str) {
int count = 0;
// 计算字符串长度,直到遇到结束符'\0'
while (*str != '\0') {
count++;
str++;
}
return count;
}
int main() {
char str[] = "Hello, World!";
int count = countCharacters(str);
printf("字符的个数为: %d\n", count);
return 0;
}
开始运行…
字符的个数为: 1664
运行结束。