strlen()计算数组的问题

coding_my_life 2012-01-29 01:07:53
本题要求:把一个单词读入字符数组,然后反向打印该单词。我的代码如下:(题中功能已实现,但请大家注意最后一个

printf语句strlen()的使用)

#include<stdio.h>
#include<string.h>

int main()
{
int index = 0,len;
char str[100];
char ch;

printf("Input the words: ");

while ((ch = getchar()) != '\n')
str[index++] = ch;

printf("\nTurn round the words: ");

for (len = index - 1;len >= 0;len--)
printf("%c",str[len]);

printf("\n");

printf("\nThe length is %d ,Why ?\n",strlen(str)); //此处我欲打印数组中单词的长度。问题有二:

// 1)如何用strlen打印单词长度,2 )strlen(len)打印的是什么长度,为什么它的值总是121 ?


return 0;

}
...全文
737 28 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
程序员小迷 2012-01-29
  • 打赏
  • 举报
回复
'\0'没加
IVERS0N 2012-01-29
  • 打赏
  • 举报
回复
遇到字符0结束
wizard_tiger 2012-01-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 joeblackzqq 的回复:]

while ((ch = getchar()) != '\n')
str[index++] = ch;

str[index] = 0; // 加上此句

strlen计算字符串长度,遇到字符'\0'结束
[/Quote]
这个正解!
ProgrammingRing 2012-01-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 joeblackzqq 的回复:]

while ((ch = getchar()) != '\n')
str[index++] = ch;

str[index] = 0; // 加上此句

strlen计算字符串长度,遇到字符'\0'结束
[/Quote]
++
小班得瑞 2012-01-29
  • 打赏
  • 举报
回复
楼上已做了正确解答,帮顶一下吧
AnYidan 2012-01-29
  • 打赏
  • 举报
回复
c 风格的字符串是以 '\0' 结尾的 char 数组,而标准库中决大多数的字符串函数 都用到了结尾的 '\0'
liru125504 2012-01-29
  • 打赏
  • 举报
回复
是因为数组str没有结束符‘\0’
JoeBlackzqq 2012-01-29
  • 打赏
  • 举报
回复
while ((ch = getchar()) != '\n')
str[index++] = ch;

str[index] = 0; // 加上此句

strlen计算字符串长度,遇到字符'\0'结束
coding_my_life 2012-01-29
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 kxalpah 的回复:]

引用 24 楼 coding_my_life 的回复:

引用 23 楼 ivers0n 的回复:

引用 22 楼 coding_my_life 的回复:




str[i++] = ch;

str[++i] = ch;


抱歉ing,原来是这样!
那为什么
char str[100] = {0};
str[++i] = ch;
他们组合,答案就始终为……
[/Quote]


理解、谢谢!因此这个数组的第一个元素一直是0,根据strlen的计算方式,以第一个\0字符作为结尾,这样长度就变为了0
JXLFZ 2012-01-29
  • 打赏
  • 举报
回复
还有++i和i++的问题。。。
JXLFZ 2012-01-29
  • 打赏
  • 举报
回复
"0"和0是不一样的,strlen是计算‘\0’前的字符个数,‘\0’的值和0一样
kxalpah 2012-01-29
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 coding_my_life 的回复:]

引用 23 楼 ivers0n 的回复:

引用 22 楼 coding_my_life 的回复:




str[i++] = ch;

str[++i] = ch;


抱歉ing,原来是这样!
那为什么
char str[100] = {0};
str[++i] = ch;
他们组合,答案就始终为 0 了呢?
[/Quote]
因为++运算符的运算优先级高于等号,因此这一句的运算顺序是先i=i+1,然后进行赋值,也就是说第一个被赋值的变量为数组的第二个元素,又因为整个数组都被初始化为0,因此这个数组的第一个元素一直是0,根据strlen的计算方式,以第一个\0字符作为结尾,这样长度就变为了0
coding_my_life 2012-01-29
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 ivers0n 的回复:]

引用 22 楼 coding_my_life 的回复:




str[i++] = ch;

str[++i] = ch;
[/Quote]

抱歉ing,原来是这样!
那为什么
char str[100] = {0};
str[++i] = ch;
他们组合,答案就始终为 0 了呢?
IVERS0N 2012-01-29
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 coding_my_life 的回复:]


[/Quote]

str[i++] = ch;

str[++i] = ch;



coding_my_life 2012-01-29
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 ivers0n 的回复:]

引用 20 楼 coding_my_life 的回复:

那这怎么解释呢?
char str[100]={0};这样初始化后计算出的答案始终为0
char str[100]="0";输入“hello”答案是6,为什么不是5?

Input the words: hello

Turn round the words: olleh

The length is 5 ……
[/Quote]
代码如下:
#include<stdio.h>
#include<string.h>
int main()

{
char str[100] = "0";
int i =0;
char ch;

while ((ch = getchar()) != '\n')

str[++i] = ch;

printf ("%d\n", strlen(str));

return 0;

}

hello
6
请按任意键继续. . .
IVERS0N 2012-01-29
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 coding_my_life 的回复:]

那这怎么解释呢?
char str[100]={0};这样初始化后计算出的答案始终为0
char str[100]="0";输入“hello”答案是6,为什么不是5?


[/Quote]

Input the words: hello

Turn round the words: olleh

The length is 5 ,Why ?
请按任意键继续. . .

我这运行正常
coding_my_life 2012-01-29
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 ivers0n 的回复:]

引用 16 楼 coding_my_life 的回复:

str[i] = '\0'; //(此句有无,答案都是5。)



char str[100] = "hello"; //剩下的被默认置0了 str[i] = '\0'; //(此句有无,答案都是5。)无所谓
[/Quote]

那这怎么解释呢?
char str[100]={0};这样初始化后计算出的答案始终为0
char str[100]="0";输入“hello”答案是6,为什么不是5?
IVERS0N 2012-01-29
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 coding_my_life 的回复:]

str[i] = '\0'; //(此句有无,答案都是5。)

[/Quote]

char str[100] = "hello"; //剩下的被默认置0了 str[i] = '\0'; //(此句有无,答案都是5。)无所谓
coding_my_life 2012-01-29
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 ivers0n 的回复:]

引用 14 楼 coding_my_life 的回复:

不,我用VS 2008,答应结果是111.(而我那个程序又是121



不置结束0 长度是随机的

定义数组时置0是个好习惯 char str[100]={0};
[/Quote]

char str[100]={0};这样初始化后计算出的答案始终为0
char str[100]="0";输入hello,答案是6,为什么不是5?
Aces 2012-01-29
  • 打赏
  • 举报
回复
strlen计算字符串长度是以0为结束的,若是你的字符串没有初始化为0,那计算出来的结果就肯定不对了!
加载更多回复(8)

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧