求字符串长度(不使用strlen函数)C语言

给定一组字符串求出长度,规定不能使用strlen函数
方法1:源码
#include<stdio.h>
int mylen(char *str)
}
int count = 0;
while (*str != ‘\0’)
}
count++;
str++;

}
return count;

}
int main()
{
int s = mylen(“fbbbbbbbbb”);
printf("%d\n", s);
return 0;
}
方法二:
int mylen(char *p)//利用指针减指针得出元素个数

{
char *start = p;//首元素地址

while (*p!='\0')
{
	p++;

}//循环结束后将p的地址
char * end = p;
return end - start;

}

猜你喜欢

转载自blog.csdn.net/Kirihara_Yukiho/article/details/123071507