首页 > 解决方案 > 有没有办法在 C 中制作这样的节日树?

问题描述

我想知道你是否可以在 C 上将这些单词重新排列成这种格式

                  Jojo|Jojo
                    is|is
                   the|the
                  Best|Best
            Intheworld|Intheworld

这是我输入的代码,但它似乎不起作用:

char a[50],b[50],c[50],d[50];

gets(a);
gets(b);
gets(c);
gets(d);    
//tried to putt it into the middle with this string
a[strcspn(a, "\n")] = 0;
b[strcspn(b, "\n")] = 0;
c[strcspn(c, "\n")] = 0;
d[strcspn(d, "\n")] = 0;    
//this is how i print it
printf("    %s|%s\n",a,a);
printf("    %s|%s\n",b,b);
printf("    %s|%s\n",c,c);
printf("    %s|%s\n",d,d);

标签: calgorithm

解决方案


不要在第一组字符串之前放置空格,而是在打印的第一个字符串上使用字段宽度。这将使给定长度的字段中的字符串右对齐:

printf("%40s|%s\n",a,a);
printf("%40s|%s\n",b,b);
printf("%40s|%s\n",c,c);
printf("%40s|%s\n",d,d);

推荐阅读