可以使用标准库函数strlen
来计算字符串的长度,示例如下:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
int length = strlen(str);
printf("Length of the string is: %d\n", length);
return 0;
}
上面的程序中,strlen
函数会返回字符串中的字符数量,不包括末尾的空字符\0
。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:c语言如何求字符串长度