【问题标题】:How to print a symbol `n` times using a `for` loop, in C?如何在C中使用`for`循环打印符号`n`次?
【发布时间】:2020-12-27 18:33:00
【问题描述】:

是否可以在 C 中使用 for 循环打印 '#' n 次?

我习惯了 Python 中的print(n * '#')

在 C 中是否有等价物?

for(i=0; i < h; i++) {
    printf("%s\n", i * h);
}

【问题讨论】:

  • 循环打印单个字符。如循环内的printf("#");
  • 一个更有趣的挑战是在不使用循环的情况下打印 n 次。

标签: c loops for-loop char putchar


【解决方案1】:

要在 for 循环中输出符号 '#' n 次,无需动态创建字符串。这将是低效的。

只要使用标准函数putchar就好了

for ( int i = 0; i < n; i++ )
{
    putchar( '#' );
}
putchar( '\n' );

这是一个演示程序。

#include <stdio.h>

int main(void) 
{
    const char c = '#';
    
    while ( 1 )
    {
        printf( "Enter a non-negative number (0 - exit): " );
        
        unsigned int n;
        
        if ( scanf( "%u", &n ) != 1  || n == 0 ) break;
        
        putchar( '\n' );
        
        for ( unsigned int i = 0; i < n; i++ )
        {
            for ( unsigned int j = 0; j < i + 1; j++ )
            {
                putchar( c );
            }
            putchar( '\n' );
        }
        
        putchar( '\n' );
    }
    return 0;
}

程序输出可能看起来像

Enter a non-negative number (0 - exit): 10

#
##
###
####
#####
######
#######
########
#########
##########

Enter a non-negative number (0 - exit): 0

如果您需要在文件流中发送符号,请使用该函数

int fputc(int c, FILE *stream);

而不是putchar

【讨论】:

  • 问题是如何构造一个重复n次的子字符串,而不是如何打印一个字符n次。
  • @Roflcopter4 重读问题“如何在 c 中使用 for 循环打印符号 # 例如 n 次”
  • 嗯。该帖子已被编辑。对不起。尽管如此,问题的核心仍然是要求替换 Python 的“foo”x 10" 语法以重复字符串。
  • @Roflcopter4 帖子已在查看其他答案后进行了编辑,我一直在观察这家伙,他是一只模仿猫。
  • @fedibenothmen 两个 for 循环与我展示的类似?
【解决方案2】:

没有。标准C中的任何机制都不可能。如果你想要一个重复n次的子字符串,你将不得不自己构造它。

简单示例:

const char *substring = "foo";

// We could use `sizeof` rather than `strlen` because it's
// a static string, but this is more general.
char   buf[8192];
size_t len = strlen(substring);
char  *ptr = buf;

// Ideally you should check whether there's still room in the buffer.
for (int i = 0; i < 10; ++i) {
    memcpy(ptr, substring, len);
    ptr += len;
}
*ptr = '\0';

printf("%s\n", buf);

【讨论】:

    【解决方案3】:

    是的,使用%.*s 格式说明符。

    int h = 10; //upper limit
    char pat[h*h];
    
    memset(pat, '#', sizeof pat);
    
    for (int i=0;i<h;i++)
    {
      printf("%.*s\n",i*h, pat);
    }
    

    输出:

    ##########
    ####################
    ##############################
    ########################################
    ##################################################
    ############################################################
    ######################################################################
    ################################################################################
    ##########################################################################################
    

    如果我误解了你的问题,请原谅。

    【讨论】:

    • 这实际上是标准格式说明符吗?我的印象是它是一个扩展。
    • 它没有列在 printf 的手册页或这里的“cplusplus.com/reference/cstdio/printf”中。我相信这是一个扩展。
    • @Roflcopter4 它被列为其中的一部分,请参阅width 表。
    • 仔细阅读。那只是为格式化指定字符串的最小宽度。
    • @user3121023 不完全是。这指定了要打印的最大字符数(除非之前找到 NUL)。您可以使用它来打印不是 NUL 终止的字符串。它不会打印字符串 n 次。
    猜你喜欢
    • 2015-06-05
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多