【发布时间】:2021-12-04 08:45:32
【问题描述】:
我想知道你是否可以在 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);
【问题讨论】:
-
旁注:使用
fgets(a, 50, stdin)而不是gets(a),因为后者不会检查a内是否有足够的空间来存储该行。 (这同样适用于其他gets()调用)。