首页 > 其他分享 >Strlen函数用法

Strlen函数用法

时间:2022-11-05 17:32:35浏览次数:59  
标签:return 函数 int Strlen 用法 char str include strlen

1.strlen的一般用法

C 库函数 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符。

#include<stdio.h>

#include<string.h>

int main()

{

char len[] = {"abcdef"};

printf("%d", strlen(len));//用strlen实现计算字符串长度

return 0;

}

2.自定义一个my_strlen函数来实现strlen的功能

#include<stdio.h>

#include<string.h>

#include<assert.h>

int my_strlen(const char *str)//传的是a的地址

{

int count=0;  //计数器

assert(str != NULL);//断延,指针必须具备有效性,不能为空指针

while (*str != '\0')

{

 count++;

 str++;

}

return count;

}

int main()

{

char len[] = {"abcdef"};

printf("%d", my_strlen(len));//用strlen实现计算字符串长度

return 0;

}

3.错误示范

错误例题1


#include<stdio.h>

#include<string.h>

int main()

{

char arr[] = { 'a','b','c','d','e','f' };

int len = strlen(arr);

printf("%d", len);

return 0;

}

计算出来的是一个随机值,原因是strlen寻找’\n‘,而'\n'在内存中不知道在哪存放;

易错易混淆知识(知识扩展)

#include<stdio.h>

#include<string.h>

int main()

{

if (strlen("abc") - strlen("abcdef") > 0)

 printf("hehe");

else

 printf("haha");

return 0;

}

运行结果如下

Strlen函数用法_#include

size_t strlen(char* str) //这是c语言规定的strlen函数的返回值

int strlen(char* str)      //这是我们写的

区别

size_t的意思是无符号整数类型,strlen("abc")计算出来的结果是3,strlen("abcdef")计算出来的结果是6, 3-6=-3,   而strlen返回的是一个无符号类型的数,所以-3在内存中的补码放入之后一定是一个大于0的数。


接下来看这样一段代码:

​#include<stdio.h>

#include<string.h>

#include<assert.h>

int my_strlen(const char* str)//传的是a的地址

{

int count = 0;  //计数器

assert(str != NULL);//断延

while (*str != '\0')

{

 count++;

 str++;

}

return count;

}

int main()

{

if (my_strlen("abc") - my_strlen("abcdef") > 0)

 printf("hehe");

else

 printf("haha");

return 0;

}

如果用自己写的my_strlen函数,因为定义的返回值是int,所以输出结果为haha;

总结:

1.字符串以‘\0'作为结束标志,strlen函数返回的是在字符串中’\0'前面出现的字符个数(不包含‘\0'),

2.参数指向的字符串必须要以’\0‘结束

3.注意函数的返回值为size_t,是无符号的(易错)

4.学会strlen函数的模拟实现


标签:return,函数,int,Strlen,用法,char,str,include,strlen
From: https://blog.51cto.com/u_15835473/5826046

相关文章

  • shell-函数学习笔记二
    shell函数的定义#方法一functionname{command...command}#方法二name(){command...command}函数的调用直接使用函数名调用,可以将函数......
  • 函数极限的性质
    函数极限的性质唯一性:若\(\lim\limits_{x\toa}f(x)=A,\\lim\limits_{x\toa}f(x)=B\),那么有\(A=B\).有界性:若\(\lim\limits_{x\toa}f(x)=A\)......
  • Python 函数
    1.1defx():foriinrange(3):print("python")print(x())1.2defx(name):foriinrange(3):print(f"python{name}。")print(x("ILOVE"))1.3defa(name,tim......
  • 介绍箭头函数的 this
    由于箭头函数不绑定this,它会捕获其所在(即定义的位置)上下文的this值,作为自己的this值1.所以call()/apply()/bind()方法对于箭头函数来说只是传入参数,对它的this......
  • Vue编程式路由导航、缓存路由组件、新的钩子函数
    1、编程式路由导航1.1作用不借助<router-link>实现路由跳转,让路由跳转更加灵活1.2具体编码//$router的两个APIthis.$router.push({ name:'xiangqing',......
  • 数组扁平化,函数柯理化
    数组扁平化将一个多维数组编程一个一维数函数柯理化将一个函数的两个参数当中两个函数一个参数来进行执行函数的作用:将多个拥有相同功能代码封装在一起,让我们写......
  • fetch的基本用法
    fetch可以更加简单的获取数据,可以看作Ajax的升级版,是基于Promise实现的1、使用语法<script>fetch('http://localhost:3000/fdata').then(function(data){......
  • asxio和fetch基本用法
    fetchFetchAPI是新的ajax解决方案Fetch会返回Promisefetch不是ajax的进一步封装,而是原生js,没有使用XMLHttpRequest对象。fetch(url,options).then()<scripttype......
  • Python基础用法
      有人说:一个人从1岁活到80岁很平凡,但如果从80岁倒着活,那么一半以上的人都可能不凡。生活没有捷径,我们踩过的坑都成为了生活的经验,这些经验越早知道,你要走的弯路就会越少......
  • 实验3 函数应用编程
     实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprin......