C: 从 char 数组打印产生错误的字符

C: Printing from char array produces erroneous characters

K.N.King 的C 编程的解决方案:现代方法,第 2 版,第 8 章,编程项目 14,产生了正确和错误的不同输出。示例如下:

Reversal of sentence: you can't swallow a cage can you?
Reversal of sentence: you can't swallow a cage can you�(�?
Reversal of sentence: you can't swallow a cage can you��x�?
Reversal of sentence: you can't swallow a cage can you�Ց�?

如示例输入所示,正确的输出应该是:

Enter a sentence: you can cage a swallow can't you?
Reversal of sentence: you can't swallow a cage can you?

我自己的解决方案和下面的解决方案(由 Github 用户@williamgherman 提供;为了便于阅读而稍作修改)都产生不同的输出。

#include <stdio.h>

int main(void)
{
    char ch, terminator, sentence[100] = {0};
    int i = 0, j;

    printf("Enter a sentence: ");
    for (i = 0; (ch = getchar()) != '\n' && i < 100; i++) {
        if (ch == '.' || ch == '!' || ch == '?') {
            terminator = ch;
            break;
        }
        sentence[i] = ch;
    }

    printf("Reversal of sentence: ");
    while (i >= 0) {
        while (sentence[--i] != ' ' && i != 0)
            ;
        j = i == 0 ? 0 : i + 1;
        while (sentence[j] != ' ' && sentence[j] != '[=12=]')
            putchar(sentence[j++]);
        if (i > 0)
            putchar(' ');
    }

    printf("%c\n", terminator);

    return 0;
}

尽管仔细检查了代码,运行 通过纸上的示例输入,我还是找不到答案。

代码如何产生这些不同的输出,正确的和不正确的?是什么产生了错误字符?

问题很可能出在用于反向打印句子的 while 循环的退出条件

    while (i >= 0) {
        while (sentence[--i] != ' ' && i != 0) ;
    ....

考虑代码将打印第一个单词 (i=3) 的情况:

  • 第二个 while 会将 i 一直递减到 0
  • 然后代码将打印单词 'you'(位置 0 到 2,包括在内)
  • 此时i=0,第一个while仍然为真
  • 第二个while将i递减为-1,并继续递减为-2、-3,直到找到space。
  • 该代码将打印索引为 -1、-2、-3 的单词,并将打印基于这些未定义值的字符串。

是的,如上面的回答所述,问题就在这个区块内

while (i >= 0) {
        while (sentence[--i] != ' ' && i != 0) ;
    ....

您应该将 i != 0 部分更改为 i >= 0,这应该可以解决问题

问题是我递减 sentence[--i] 发生在 之前 i != 0 所以想象一下从 i = 0 开始的 while 循环 - 这意味着 sentence[i - 1] 将(可能)不是 ' ' 并且 i 不会等于零。

对于初学者来说,程序有未定义的行为,因为变量 terminator 没有被初始化。用户可以在不提供这些字符之一的情况下按 Enter 键 ".!?"

for (i = 0; (ch = getchar()) != '\n' && i < 100; i++) {
                             ^^^^^^^                       

在这种情况下这个语句

printf("%c\n", terminator);

将输出一个不确定的值。

此外,用户可以通过按下相应的组合键来中断循环,但循环不会处理这种情况。

如果用户在循环的最开始按下 Enter 键,那么 i 将等于 0 在这种情况下,内部 while 循环

while (i >= 0) {
    while (sentence[--i] != ' ' && i != 0)
                    ^^^
        ;

将调用未定义的行为。

此外,在终止字符 (".!?") 之前,句子可以包含 spaces。因此这个循环

    while (sentence[--i] != ' ' && i != 0)
        ;
    j = i == 0 ? 0 : i + 1;

再次将被错误终止。即 j 将等于 i + 1 其中存储了一个零字符(前提是用户没有输入数组句子的所有 100 个元素)。

除此之外,程序不会输出单词之间存在的 space 数量。它只尝试输出一个 space

putchar(' ');

考虑到用户可以输入例如制表符 '\t' 而不是 space 字符 ' '.

下面显示了如何编写程序。

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

int main(void) 
{
    const char *punctuation = ".?!";

    enum { N = 100 };
    char s[N] = "";
    char terminator = '[=15=]';
    
    printf( "Enter a sentence: " );
    
    size_t n = 0;

    for ( int c; n < N && ( c = getchar() ) != EOF && c != '\n'; n++ )
    {
        if ( strchr( punctuation, c ) )
        {
            terminator = c;
            break;
        }
        
        s[n] = c;
    }
    
    printf( "Reversal of sentence: " );

    putchar( '\"' );
    
    size_t i = 0;
    
    while ( i != n && isblank( ( unsigned char )s[i] ) ) putchar( s[i++] );
    
    for ( size_t j = n; i != n; )
    {
        while ( i != n && !isblank( ( unsigned char )s[i] ) ) i++;
        
        while ( isblank( ( unsigned char )s[j-1] ) ) j--;

        size_t k = j;
        while ( j != 0 && !isblank( ( unsigned char )s[j-1] ) ) j--;
                
        for ( size_t l = j; l != k; l++ ) putchar( s[l] );

        while ( i != n && isblank( ( unsigned char )s[i] ) ) putchar( s[i++] );
    }
    
    if ( terminator ) putchar( terminator );
    putchar( '\"' );
    
    return 0;
}

程序输出可能看起来像

Enter a sentence: you can  cage   a    swallow     can't      you?
Reversal of sentence: "you can't  swallow   a    cage     can      you?"

如您所见,程序保留了输入句子中包含的所有 space。