微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在不同的行中打印一串名称

如何解决在不同的行中打印一串名称

我最近开始学习 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;
}

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

解决方法

     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');
,

除了其他答案之外,您还可以使用 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
,

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

其次,您的测试条件是错误的,并且永远不能为假:字符不能同时是制表符空格,因此!=测试将永远为真;要解决此问题,请将 || 更改为 &&

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;
}
,

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

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

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

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

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

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

#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

如果要包含标题 <ctype.h>

#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' );
    }
}
//...
,

你想要的是函数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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。