C语言字符串函数----strlen函数用法

字符串函数<string.h>-----strlen函数

strlen()函数

  • strlen函数用来统计字符串的长度.
    遇到’/0’停止,但是不包括’/0’.

-其函数原型为 unsigned int strlen (char *s);

#include <stdio.h>
#include <string.h>
main()
{
char str1[5]="aaaa";//不能有5个a,必须留一个位置放置'/0'
char *str2 = "aaa";

printf("%d ",strlen(str1));
printf("%d\n",strlen(str2));
} 
//结果为 4 3
  • 自定义函数实现strlen函数
int len(char *str)
{
 int i = 0;
    while(str[i]!='\0')
     i++;
    return i;
}

具体详细内容可以看C Primer Plus  702页B.5.22

2019/12/26 18/00

发布了21 篇原创文章 · 获赞 5 · 访问量 747

猜你喜欢

转载自blog.csdn.net/weixin_45862170/article/details/103720022