C语言 在 C 中将文本右对齐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25626851/
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
Align text to right in C
提问by Salman2013
I am little struggling how to make my output to show like this:
如何让我的输出显示如下:
a
aa
aaa
My current output shows this instead:
我当前的输出显示了这一点:
a
aa
aaa
Below are my code:
下面是我的代码:
void displayA(int a){
for(int i = 0; i < a; i++)
printf("a");
}
int main(void){
displayA(1);
printf("\n");
displayA(2);
printf("\n");
displayA(3);
printf("\n");
return 0;
}
Any suggestion? Thanks.
有什么建议吗?谢谢。
Thanks for the answer. I realized that my coding logic was wrong. Using the suggestion below helped me figure it out. Thanks!
谢谢你的回答。我意识到我的编码逻辑是错误的。使用下面的建议帮助我弄清楚了。谢谢!
回答by Don't You Worry Child
回答by thelaws
Here the parameter wdetermines the character width to align the a's to
这里的参数w决定了字符宽度以将a's对齐到
void displayA(int a, int w){
int i;
for(i = 0; i < w; i++) {
if( i < w - a ) printf(" ");
else printf("a");
}
}
回答by ravi
This may be helpful.
这可能会有所帮助。
To print staircasepattern:
打印楼梯图案:
#
##
###
####
#####
######
Following are the codes:
以下是代码:
#include<stdio.h>
void print(int n)
{ int i,j;
for(i=1;i<=n;i++)
{ printf("%*s",n-i,""); //for right alignment by n-1 spaces.
for(j=0;j<i;j++)
{
printf("%s","#");
}
printf("\n");
}
}
int main(void)
{
print(6);
system("pause");
return 0;
}

