关于strlen()函数,CPP chapter6 编程练习题

我有鱼丸粗面 2016-04-27 11:28:02
题目:编写一个程序读入一行输入,然后反向打印该行。您可以把输入存储在一个char数组中;假定该行不超255个字符。
#include <strings.h>
#include <stdio.h>
int main(void)
{
char input[255];
char ch;
int i, length;

printf("Please enter a word: \n");
scanf("%c", &ch);
i = 0;
while (ch != '\n')
{
input[i] = ch;
scanf("%c", &ch);
i++;
}
length = strlen(input);
for (i = length-1; i >=0; i--) // 初始化语句 i -= 1 则正常;
printf("%c", input[i]);
printf("\n");

return 0;


问题:其中strlen(input),在调戏过程中,发现输入超过一定字符后,返回值的不正确,比实际的要大,导致最后printf()输出不正确?谁能告诉我这是怎么回事吗?
...全文
193 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
江南大富翁 2016-04-29
  • 打赏
  • 举报
回复
char input[255] = {0}; 因为你定义数组的时候分配给的这段内存里面可能是有任意数值的,比如别的程序刚释放的内存,length计算的值就可能会比实际的大一些
赵4老师 2016-04-28
  • 打赏
  • 举报
回复
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\strlen.c
/***
*strlen.c - contains strlen() routine
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       strlen returns the length of a null-terminated string,
*       not including the null byte itself.
*
*******************************************************************************/

#include <cruntime.h>
#include <string.h>

#pragma function(strlen)

/***
*strlen - return the length of a null-terminated string
*
*Purpose:
*       Finds the length in bytes of the given string, not including
*       the final null character.
*
*Entry:
*       const char * str - string whose length is to be computed
*
*Exit:
*       length of the string "str", exclusive of the final null byte
*
*Exceptions:
*
*******************************************************************************/

size_t __cdecl strlen (
        const char * str
        )
{
        const char *eos = str;

        while( *eos++ ) ;

        return( eos - str - 1 );
}
小灸舞 2016-04-28
  • 打赏
  • 举报
回复
char input[255] = {0};就是将每个字符初始化成'\0'了(0和'\0'是一样的,但和'0'不一样,注意区分)
strlen是计算到下一个\0之间的字符数量,如果没有初始化又没有手动补\0
不初始化的话,字符数组原来存的是垃圾值,假设是aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
然后楼主输入了sdmfn,那么字符数组就变成了sdmfnaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
strlen不会说遇到a就停止下来了,会继续往下的,所以要手动补\0变成sdmfn\0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
我有鱼丸粗面 2016-04-28
  • 打赏
  • 举报
回复
引用 2 楼 qq423399099 的回复:
input初始化成0:char input[255] = {0}; 或者for循环结束手动补上\0
谢谢回答!for循环结束补上\0,这个不懂。 还有能解释一下这是什么问题吗?为什么input初始化为{0},和for循环结束补上\0,则正常!
我有鱼丸粗面 2016-04-28
  • 打赏
  • 举报
回复
引用 1 楼 renwotao2009 的回复:
input数组初始化为\0
谢谢!能解释一下吗?
小灸舞 2016-04-28
  • 打赏
  • 举报
回复
input初始化成0:char input[255] = {0}; 或者for循环结束手动补上\0
我有鱼丸粗面 2016-04-28
  • 打赏
  • 举报
回复
引用 5 楼 qq423399099 的回复:
char input[255] = {0};就是将每个字符初始化成'\0'了(0和'\0'是一样的,但和'0'不一样,注意区分) strlen是计算到下一个\0之间的字符数量,如果没有初始化又没有手动补\0 不初始化的话,字符数组原来存的是垃圾值,假设是aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 然后楼主输入了sdmfn,那么字符数组就变成了sdmfnaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa strlen不会说遇到a就停止下来了,会继续往下的,所以要手动补\0变成sdmfn\0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
讲得很详细,明白了!谢谢!
我有鱼丸粗面 2016-04-28
  • 打赏
  • 举报
回复
引用 7 楼 renwotao2009 的回复:
[quote=引用 4 楼 a135312 的回复:] [quote=引用 1 楼 renwotao2009 的回复:] input数组初始化为\0
谢谢!能解释一下吗?[/quote]char input[255] = {0};就行了,或者char input[255] = {'\0'};[/quote] 嗯!明白了!谢谢!
renwotao2009 2016-04-28
  • 打赏
  • 举报
回复
引用 4 楼 a135312 的回复:
[quote=引用 1 楼 renwotao2009 的回复:] input数组初始化为\0
谢谢!能解释一下吗?[/quote]char input[255] = {0};就行了,或者char input[255] = {'\0'};
renwotao2009 2016-04-27
  • 打赏
  • 举报
回复
input数组初始化为\0

33,317

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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