21xrx.com
2024-09-19 09:05:16 Thursday
登录
文章检索 我的文章 写文章
C语言字符串常用函数-strlen
2023-06-15 10:59:52 深夜i     --     --
C语言 字符串 函数 strlen 长度 字符个数

在C语言中,字符串是由一系列字符组成的,字符串最后以‘\0’结尾。strlen()是C语言中常用的字符串函数之一,用来返回一个字符串的长度,即字符串中字符的个数,但不包括字符串末尾的‘\0’。该函数的实现方法是从字符串的首地址开始遍历,直到找到‘\0’为止,并统计遍历过的字符数量。

例如,在下面这段代码中:


#include

#include

int main() {

  char str[] = "Hello, world!";

  int len = strlen(str);

  printf("The length of the string is %d\n", len);

  return 0;

}

输出结果为“The length of the string is 13”,即字符串“Hello, world!”中共有13个字符(不包括末尾的‘\0’)。

除了strlen()函数,C语言中还有许多其他的字符串函数,如strcpy()、strncpy()、strcat()、strncat()等等,它们可以用来复制、拼接、截取、比较字符串等操作。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复