您的位置:首页 > 其它

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

2014-08-01 13:57 477 查看
写自己的 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[100];
//  gets(s);
fgets(s, N, stdin);                 //(gets和fgets函数的区别)
if(s[strlen(s) - 1] == '\n') {      // 去掉换行符
s[strlen(s) - 1] = '\0';
}
printf("%d", mylen(s));

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐