20170912_字符串长度strlen实现

来源:互联网 发布:剑三莫雨捏脸数据截图 编辑:程序博客网 时间:2024/06/07 04:48

20170912_字符串长度strlen实现


//strlen()函数求解字符数组的长度,以‘/0’作为结束符.//但是,是计算不为‘/0’的数组元素个数。#include<iostream>#include<cstdio>#include<cassert>using namespace std;int strLen(const char *p)//形式参数不容改变{assert(p!=NULL);//判断字符指针是否是空//if(p==NULL)//return 0;int len=0;while(*p != '\0'){++len;++p;}return len;//要有返回值}int main(){char *str[]={"","a","abcdef","abc def"};for(int i=0; i<4; ++i){cout<<"Input string is: "<<str[i]<<endl;int len=0;len=strLen(str[i]);cout<<"Length is: "<<len<<endl<<endl;}system("pause");return 0;}





原创粉丝点击