【问题标题】:putchar() function: ambiguous outputputchar() 函数:模棱两可的输出
【发布时间】:2017-01-01 01:12:07
【问题描述】:

这是一个简单的代码,试图从字符数组中清除空格,但输出与我预期的“YasserMohamed”不同。

#include<stdio.h>

int main()
{
    char x[]="Yasser Mohamed";
    char ch;
    int i=0;
    while (x[i]!='\n')
    {
        if(x[i]!=' ')
            putchar(x[i]);
        i++;
    }
    system("pause");

    return 0 ;
}

【问题讨论】:

  • 考虑其他答案也可以标记最佳答案。
  • system() 函数需要声明:#include &lt;stdlib.h&gt; 缺少发布的代码。
  • pause shell 命令不可移植,只能在 windows 上找到。实现类似功能的通用方法是:int ch; while( (ch = getchar() != EOF &amp;&amp; '\n' != ch ); getchar();

标签: c arrays string putchar


【解决方案1】:

x 中没有换行符 ('\n')。所以,条件不对,应该是:

while (x[i]) /* until the null byte is found */
{
    if (x[i] != ' ')
        putchar(x[i]);
    i++;
}

【讨论】:

  • 注意:写x[i] != '\0'更明确,但可能导致写x[i] != '0'
【解决方案2】:

x 中的字符串不包含您在循环中用作条件的换行符 '\n'

使用while (x[i]!=0x00) 以终止NUL 字符(0x00) 结束。

【讨论】:

  • "使用while (x[i]!=0x00)" 或while (x[i]!=0)while (x[i]!='\0') 或只使用while (x[i])
【解决方案3】:

那是因为你从来没有停止过你写的循环

while(x[i]!='\n')
    {
       //What You Want To Do.
           }

但是对于任何定义的x[i]x[i] 都不是'\n'

如果你把它写成i!= 14,它会起作用。然后循环会停在你名字的末尾。超越是未定义的,因为这不是您的可变内存区域。

或者您也可以将 while(x[i]) 写成 C 中字符串的结尾是 Null-Terminated \0,其计算结果为 false,因此循环将停止。

正确的代码可能是

 #include<stdio.h>
 int main()
 {

     char x[]="Yasser Mohamed";
     char ch;
     int i=0;
     while (x[i])    //As Null Character '\0' evaluates to false it would stop the loop
     {
         if(x[i]!=' ')
             putchar(x[i]);
         i++;
     }
     system("pause");

     return 0 ;

 }

【讨论】:

    【解决方案4】:

    原来的x中没有\n,所以你就继续迭代未初始化的内存,直到碰巧遇到\n。相反,您应该迭代到字符串终止字符 - \0:

    while (x[i] != '\0') {
    // Here --------^
    

    【讨论】:

      【解决方案5】:

      你也可以用0代替'\0'(是完全相同的值),像这样:

      for (int i = 0; x[i] != 0; i++) {
          if (x[i] != ' ')
              putchar(x[i]);
      }
      

      【讨论】:

        【解决方案6】:

        在以空字符结尾的字符串末尾有一个空字符,而不是新行。

        您应该将'\n' 更改为'\0' 或0(这是空字符的ASCII 码)。

         #include<stdio.h>
        
        
         int main()
         {
        
             char x[]="Yasser Mohamed";
             char ch;
             int i=0;
             while (x[i]!='\0')
             {
                 if(x[i]!=' ')
                     putchar(x[i]);
                 i++;
             }
             system("pause");
        
             return 0 ;
        
         }
        

        【讨论】:

          【解决方案7】:

          更新代码:

          int main()
          {
              char x[]="Yasser Mohamed";
              char ch;
              int i=0;
              while (x[i]!='\0')
              {
                  if(x[i]!=' ') {
                      printf("%c", x[i]); // replace putchar with printf
                      fflush(stdout); // force character to appear
                  }
                  i++;
              }
              printf("\n"); // print newline so shell doesn't appear right here
              return 0 ;
          }
          

          字符串以空 \0 字符而不是换行符结束。

          此外,您应该添加一个fflush 语句(至少在 linux 上)以确保打印每个字符。

          为了让你的输出看起来不错,在循环之后添加一个换行符。

          我用printf 替换了你的putchar 调用,看看在我运行你的程序时这是否有帮助。 putchar 可能也可以正常工作。

          我删除了system(pause),因为它似乎没有帮助。我改为添加换行符打印。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-02-13
            • 2014-08-12
            • 1970-01-01
            • 2017-03-13
            • 2015-08-18
            • 1970-01-01
            相关资源
            最近更新 更多