【问题标题】:Printing a string of names in different lines在不同的行中打印一串名称
【发布时间】:2021-03-31 09:14:49
【问题描述】:

我最近开始学习 C,并编写了一小段代码,但它并没有真正按照我想要的方式工作:

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

int main()
{
    int i;
    char a[] = "Bill Sarah Alice";
    for (i = 0; a[i] != '\0'; i++)
    {
        if (a[i] != '\t' || a[i] != ' ')
        {
            printf("%c", a[i]);
        }
        putchar('\n');
    }
    return 0;
}

我想每行打印一个名称,但改为每行打印一个字符。有人能告诉我应该如何修复它才能正常工作吗?

【问题讨论】:

  • putchar 放入else 块中。

标签: arrays c loops c-strings putchar


【解决方案1】:
     if(a[i] != '\t' || a[i] != ' '){

每个字符都不是制表符或空格。所以这个if 将被每个字符传递。然后在每个字符后输出一个换行符。所以这段代码等价于:

  for(i=0; a[i] != '\0'; i++){
     printf("%c", a[i]);
     putchar('\n');
  }

如果你想在一行上打印每个名字,你需要做更多这样的事情:

  for(i=0; a[i] != '\0'; i++){
     if(a[i] == '\t' || a[i] == ' ')
        putchar('\n');
     else
        printf("%c", a[i]);
  }
  putchar('\n');

【讨论】:

    【解决方案2】:

    除了其他答案,您还可以使用isspace 来检测空格或制表符:

    #include <ctype.h> // for isspace()
    #include <stdio.h> 
    
    
    int main()
    {
        int i;
        char a[] = "Bill Sarah Alice";
        for (i = 0; a[i] != '\0'; i++)
        {
            if (!isspace(a[i])) // if it's not a space
            {
                printf("%c", a[i]); // print character
            }
            else
                putchar('\n'); // if it is print a newline
        }
    }
    

    输出:

    Bill
    Sarah
    Alice
    

    【讨论】:

    • 我可能会推荐isspace,因为它也会寻找新行。不适用于这种特定情况,但只要您从标准输入等获取输入。
    • @Lundin,是的,这样更合适。
    【解决方案3】:

    您的代码中有两个问题。首先,换行符将打印在 每个 运行通过for 循环;要解决此问题,请将 putchar('\n'); 行放入 else 块中。

    其次,你的测试条件是错误的,永远不可能是假的:字符不能同时是制表符空格,所以!=测试之一将永远是真实的;要解决此问题,请将 || 更改为 &amp;&amp;

    int main()
    {
        int i;
        char a[] = "Bill Sarah Alice";
        for (i = 0; a[i] != '\0'; i++) {
            if (a[i] != '\t' && a[i] != ' ') {
                printf("%c", a[i]);
            }
            else {
                putchar('\n');
            }
        }
        return 0;
    }
    

    【讨论】:

      【解决方案4】:

      在 for 循环中,在打印字符串中不等于 '\t'' ' 的每个字符后,将执行换行符 '\n' 的输出。

           if(a[i] != '\t' || a[i] != ' '){
              printf("%c", a[i]);
           }
           putchar('\n');
      

      而且if语句中的条件至少应该写成这样

           if(a[i] != '\t' && a[i] != ' '){
                          ^^^^
      

      需要在输出全名后输出换行符。因此,如果您要逐个字符地输出名称,那么您还需要一个内部循环。

      标题&lt;string.h&gt; 也是多余的,因为您的程序中没有使用标题中的声明。所以你可以删除指令

      #include <string.h>
      

      该算法可以如下面的演示程序所示。它被实现为一个单独的函数。

      #include <stdio.h>
      
      void print_names( const char *s )
      {
          while ( *s != '\0' )
          {
              while ( *s == ' ' || *s == '\t' ) ++s;
              while ( *s && *s != ' ' && *s != '\t' ) putchar( *s++ );
              putchar( '\n' );
          }
      }
      
      int main(void) 
      {
          char a[] = "Bill Sarah Alice";
      
          print_names( a );
          
          return 0;
      }
      

      程序输出是

      Bill
      Sarah
      Alice
      

      如果包含标题&lt;ctype.h&gt;

      #include <ctype.h>
      

      然后可以使用标准 C 函数 isblank 重写该函数,该函数本身会检查字符是否等于空格字符或制表符。

      #include <stdio.h>
      #include <ctype.h>
      
      void print_names( const char *s )
      {
          while ( *s != '\0' )
          {
              while ( isblank( ( unsigned char )*s ) ) ++s;
              while ( *s && !isblank( ( unsigned char )*s ) ) putchar( *s++ );
              putchar( '\n' );
          }
      }
      //...
      

      【讨论】:

        【解决方案5】:

        你想要的是函数strtok(3)

        strtok 使用分隔符字符串返回部分字符串。通过使用“”作为分隔符,您可以轻松提取名称。有关如何使用该功能,请阅读我上面链接的手册页。或阅读this 文章。

        您可以使用以下代码一次打印一个名称

        #include <stdio.h>
        #include <string.h>
        
        int main
        (int argc, char *argv[], char *envp[])
        {
                char a[] = "Bill Sarah Alice";
                char *tok = strtok (a, " ");
                while (tok) {
                        puts (tok);
                        tok = strtok (NULL, " ");
                }
                return 0;
        }
        

        【讨论】:

        • strtok 的问题在于它破坏了原始字符串。因此,如果将代码更改为char* a,您会突然崩溃。它是导致问题多于解决问题的功能之一。
        猜你喜欢
        • 1970-01-01
        • 2016-07-13
        • 2021-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-20
        • 1970-01-01
        • 2013-04-05
        相关资源
        最近更新 更多