指针练习

#include <stdio.h>

/*

 (不包括\0)

 编写一个int string_len(char *s),返回字符串s的字符长度

 

 */

int string_len(char *s);

 

int main()

{

    //char *name = "itlance";

    

   // 男 \u434\u4343\u434

    

    int size = string_len("tre777");

    

    printf("%d\n", size);

    return 0;

}

 

int string_len(char *s)

{

    // 1.定义一个新的指针变量指向首字符

    char *p = s;

    

    /*

    while ( *s != '\0' )

    {

        s++;

    }*/

    

    while ( *s++ ) ; 

    

    return s - p - 1;

}

 

/*

int string_len(char *s)

{

    // 记录字符的个数

    int count = 0;

    

    // 如果指针当前指向的字符不是'\0'

    // 首先*s取出指向的字符

    // 然后s++

    while ( *s++ )

    {

        // 个数+1

        count++;

        

        // 让指针指向下一个字符

        //s = s + 1;

        //s++;

    }

    

    return count;

}

*/

 

/*

int string_len(char *s)

{

    // 记录字符的个数

    int count = 0;

    

    // 如果指针当前指向的字符不是'\0'

    while ( *s != '\0')

    {

        // 个数+1

        count++;

        

        // 让指针指向下一个字符

        //s = s + 1;

        s++;

    }

    

    return count;

}*/

posted @ 2016-03-28 09:50  lance.xiang  阅读(74)  评论(0)    收藏  举报