scanf()为什么在这个程序中忽略了空格后面的部分,以反转字符串?

huangapple go评论75阅读模式
英文:

Why is scanf() ignoring the part after the space in this program to reverse a string?

问题

当我运行这段代码时,如果我输入123,它会返回321,但当我输入123 325时,它只返回321作为输出(应该是523 321)。为什么会这样呢?

英文:
#include <stdio.h>

#include <string.h>

main() {
    int i;
    char given[30];
    char reversed[30];
    printf("enter your stringn");
    scanf("%s", given);
    for (i = 0; i <= (strlen(given)); i++) {
        if (i <= (strlen(given) - 1)) {
            reversed[i] = given[(strlen(given) - 1 - i)];
        } else {
            reversed[i] = given[i];
        }
    }
    printf("%s", reversed);
    return 0;
}

When I run this code, if I give input as 123 its giving back 321 but when I give 123 325 as input it is giving just 321 as output (it should be 523 321). Why is this so?

答案1

得分: 2

%s 不会读取整行。请查看这里。关于 %s 的说明是:任意数量的非空白字符,停止于找到第一个空白字符。在存储序列的末尾自动添加终止的空字符。

也许如何允许使用 scanf 输入空格?如何使用 < 从文本文件中读取整行?可以指导您寻找替代方法。

英文:

%s does not read a whole line. Take a look here. What the entry for %s says is Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.

Maybe How do you allow spaces to be entered using scanf? or How to read one whole line from a text file using <? can point you to alternatives.

答案2

得分: 2

首先,函数main应明确指定返回类型为int(或兼容类型)

int main(void)

至于你的问题,根据C标准(7.21.6.2 fscanf函数scanf也适用)

s 匹配一系列非空白字符。

这意味着一旦遇到空白字符,用作参数表达式的相应字符数组的填充就会停止。非空白字符的读取序列以空字符\0结尾。因此,程序只会读取用户输入的第一个单词123并对其进行反转,如果你有一个循环,那么输入行的其余部分将由另一个scanf()调用读取。

如果要读取包含嵌入的空白字符的字符串,那么你需要使用另一种转换规范,例如:

scanf(" %29[^\n]", given);

还要注意,多次调用strlen函数是低效的。

另外,你应该在使用它们的最小作用域中声明变量。

你的程序可以如下所示:

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

int main(void) 
{
    char given[30];
    char reversed[30];

    printf("enter your string: ");
    
    if (scanf(" %29[^\n]", given) == 1) 
    {
        size_t n = strlen(given);
        reversed[n] = '
#include <stdio.h>
#include <string.h>

int main(void) 
{
    char given[30];
    char reversed[30];

    printf("enter your string: ");
    
    if (scanf(" %29[^\n]", given) == 1) 
    {
        size_t n = strlen(given);
        reversed[n] = '\0';

        for (size_t i = 0; i < n; i++) 
        {
            reversed[i] = given[n - 1 - i];
        }

        puts(reversed);
    }

    return 0;
}
'
;
for (size_t i = 0; i < n; i++) { reversed[i] = given[n - 1 - i]; } puts(reversed); } return 0; }
英文:

For starters the function main shall have explicitly specified return type int (or compatible)

int main( void )

As for your question then according to the C Standard (7.21.6.2 The fscanf function; the same is valid for scanf)

> s Matches a sequence of non-white-space characters.

That is as soon as a white space character is encountered filling of the corresponding character array used as an argument expression is stopped. The read sequence of non white space characters is ended with a null character &#39;\0&#39;. Thus only the first word 123 from user input is read and reversed by the program, the rest of the input line would be read by another call to scanf() if you had a loop.

If you want to read strings that contain embedded white space characters, then you need to use another conversion specification as for example:

scanf(&quot; %29[^\n]&quot;, given);

Pay attention too that calling the function strlen several times is inefficient.

Also you should declare variables in minimum scopes where they are used.

Your program can look the following way

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

int main( void ) 
{
    char given[30];
    char reversed[30];

    printf( &quot;enter your string: &quot; );
    
    if ( scanf(&quot; %29[^\n]&quot;, given) == 1 )
    {
        size_t n = strlen( given );
        reversed[n] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
int main( void ) 
{
char given[30];
char reversed[30];
printf( &quot;enter your string: &quot; );
if ( scanf(&quot; %29[^\n]&quot;, given) == 1 )
{
size_t n = strlen( given );
reversed[n] = &#39;\0&#39;;
for ( size_t i = 0; i &lt; n; i++ ) 
{
reversed[i] = given[n - 1 - i];
}
puts( reversed );
}
return 0;
}
&#39;; for ( size_t i = 0; i &lt; n; i++ ) { reversed[i] = given[n - 1 - i]; } puts( reversed ); } return 0; }

huangapple
  • 本文由 发表于 2023年6月29日 23:11:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定