C语言 如何使用 printf 重复一个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14678948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:13:59  来源:igfitidea点击:

How to repeat a char using printf?

cprintf

提问by Muis

I'd like to do something like printf("?", count, char)to repeat a character counttimes.

我想做一些事情,比如printf("?", count, char)重复一个角色的count时间。

What is the right format-string to accomplish this?

完成此操作的正确格式字符串是什么?

EDIT: Yes, it is obvious that I could call printf()in a loop, but that is just what I wanted to avoid.

编辑:是的,很明显我可以printf()在循环中调用,但这正是我想要避免的。

采纳答案by synthesizerpatel

Short answer - yes, long answer: not how you want it.

简短的回答 - 是的,长的回答:不是你想要的。

You can use the %* form of printf, which accepts a variable width. And, if you use '0' as your value to print, combinedwith the right-aligned text that's zero padded on the left..

您可以使用 %* 形式的printf,它接受可变宽度。并且,如果您使用“0”作为打印值,并结合在左侧填充零的右对齐文本。

printf("%0*d\n", 20, 0);

produces:

产生:

00000000000000000000

With my tongue firmly planted in my cheek, I offer up this little horror-show snippet of code.

我的舌头牢牢地贴在我的脸颊上,我提供了这个小小的恐怖表演代码片段。

Some times you just gotta do things badlyto remember why you try so hard the rest of the time.

有时候,你只需要把事情做得很糟糕,就可以记住为什么你在剩下的时间里那么努力。

#include <stdio.h>

int width = 20;
char buf[4096];

void subst(char *s, char from, char to) {
    while (*s == from)
    *s++ = to;
}

int main() {
    sprintf(buf, "%0*d", width, 0);
    subst(buf, '0', '-');
    printf("%s\n", buf);
    return 0;
}

回答by rep_movsd

You can use the following technique:

您可以使用以下技术:

printf("%.*s", 5, "=================");

This will print "=====" It works for me on Visual Studio, no reason it shouldn't work on all C compilers.

这将打印"=====" 它在 Visual Studio 上对我有用,没有理由它不适用于所有 C 编译器。

回答by Ariel

If you limit yourself to repeating either a 0 or a space you can do:

如果您限制自己重复 0 或空格,则可以执行以下操作:

For spaces:

对于空间:

printf("%*s", count, "");

For zeros:

对于零:

printf("%0*d", count, 0);

回答by Kristian Gregersen

In c++ you could use std::string to get repeated character

在 C++ 中,您可以使用 std::string 来获取重复字符

printf("%s",std::string(count,char).c_str());

For example:

例如:

printf("%s",std::string(5,'a').c_str());

output:

输出:

aaaaa

回答by Mats Petersson

There is no such thing. You'll have to either write a loop using printfor puts, or write a function that copies the string count times into a new string.

哪有这回事。您必须使用printfor编写一个循环puts,或者编写一个函数来将字符串 count 次复制到一个新字符串中。

回答by Keith Thompson

printfdoesn't do that -- and printfis overkill for printing a single character.

printf不这样做 - 并且printf对于打印单个字符来说太过分了。

char c = '*';
int count = 42;
for (i = 0; i < count; i ++) {
    putchar(c);
}

Don't worry about this being inefficient; putchar()buffers its output, so it won't perform a physical output operation for each character unless it needs to.

不要担心这会效率低下;putchar()缓冲其输出,因此除非需要,否则不会对每个字符执行物理输出操作。

回答by ramon

If you have a compiler that supports the alloca() function, then this is possible solution (quite ugly though):

如果您有一个支持 alloca() 函数的编译器,那么这是可能的解决方案(虽然很丑陋):

printf("%s", (char*)memset(memset(alloca(10), '
for (int i = 0;  i < 10;  i++, printf("%c", 'x'));
', 10), 'x', 9));

It basically allocates 10 bytes on the stack which are filled with '\0' and then the first 9 bytes are filled with 'x'.

它基本上在堆栈上分配 10 个字节,用 '\0' 填充,然后前 9 个字节用 'x' 填充。

If you have a C99 compiler, then this might be a neater solution:

如果您有 C99 编译器,那么这可能是一个更简洁的解决方案:

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

void repeat_char(unsigned int cnt, char ch) {
    char buffer[cnt + 1];
    /*assuming you want to repeat the c character 30 times*/
    memset(buffer,ch,cnd); buffer[cnt]='
#include <stdio.h>

void repeat (char input , int count )
{
    for (int i=0; i != count; i++ )
    {
        printf("%c", input);
    }
}

int main()
{
    repeat ('#', 5);
    return 0;
}
'; printf("%s",buffer) }

回答by Pavlos Fragkiadoulakis

#####

回答by Loay Hussein

you can make a function that do this job and use it

你可以制作一个功能来完成这项工作并使用它

printf("%.*s\n",n,(char *) memset(buffer,c,n));

This will output

这将输出

##代码##

回答by Rene

##代码##

n<= sizeof(buffer)[ maybe also n < 2^16]

n<= sizeof(buffer)[ 也可能 n < 2^16]

However the optimizer may change it to puts(buffer)and then the lack of EoS will .....

但是优化器可能会将其更改为puts(buffer)然后缺少 EoS 将.....

And the assumption is that memset is an assembler instruction (but still a loop be it on chip).

并且假设 memset 是一个汇编指令(但在芯片上仍然是一个循环)。

Strictly seen there is no solution given you precondition 'No loop'.

严格来说,给定“无循环”的前提条件,没有解决方案。