您的位置:首页 > 其它

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

2014-05-27 13:11 106 查看
#include <stdio.h>

/*

(不包括\0)

编写一个int string_len(char *s),

返回字符串s的字符长度

*/

int string_len(char *s);

int main()

{

//char *name = "itcast";

// 男 \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;

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