编写一个程序实现strlen函数的功能

时间:2023-03-09 15:47:29
编写一个程序实现strlen函数的功能

写自己的 strlen 函数-----→ mystrlen

 #include <stdio.h>
#include <string.h>
#define N 100 int mylen(char *s)
{
//数组型
// int cnt = 0;
// while(s[cnt] != '\0') {
// cnt++;
// } // return cnt; //指针型
char *p = s;
while(*s != '\0') {
s++;
} return s - p;
} int main()
{
char s[];
// gets(s);
fgets(s, N, stdin); //(gets和fgets函数的区别
if(s[strlen(s) - ] == '\n') { // 去掉换行符
s[strlen(s) - ] = '\0';
}
printf("%d", mylen(s)); return ;
}

相关文章