用函数来编写实现strlen()函数功能

strlen( )函数:

   测试字符串实际长度的函数,它的返回值是字符串中字符的个数(不包含’\0’)

 

//strlen( )函数:测试字符串实际长度的函数,它的返回值是字符串中字符的个数(不包含’\0’)
//模拟strlen( )函数功能
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int my_strlen(char str[])
{
    int i = 0;
    while (str[i] != '\0')
    {
        i++;
    }

    return i;
}

int main()
{
    char strHello[] = "hello world";
    int nNum1, nNum2;
    nNum1 = strlen(strHello);
    nNum2 = my_strlen(strHello);

    printf("nNum1 = %d\r\nnNum2 = %d\r\n", nNum1, nNum2);
    system("pause");
    return 0;
}

 

posted on 2017-12-13 21:48  秋雨丶梧桐  阅读(707)  评论(0编辑  收藏  举报

导航