我想知道你是否可以在 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);
uj5u.com熱心網友回復:
不要在第一組字串之前放置空格,而是在列印的第一個字串上使用欄位寬度。這將右對齊給定長度的欄位中的字串:
printf("@s|%s\n",a,a);
printf("@s|%s\n",b,b);
printf("@s|%s\n",c,c);
printf("@s|%s\n",d,d);
uj5u.com熱心網友回復:
#include <stdio.h>
#include <string.h>
int main()
{
char a[50],b[50],c[50],d[50];
fgets(a,50,stdin);
fgets(b,50,stdin);
fgets(c,50,stdin);
fgets(d,50,stdin);
a[strcspn(a, "\n")] = 0;
b[strcspn(b, "\n")] = 0;
c[strcspn(c, "\n")] = 0;
d[strcspn(d, "\n")] = 0;
printf("s|%s\n",a,a);
printf("s|%s\n",b,b);
printf("s|%s\n",c,c);
printf("s|%s\n",d,d);
return 0;
}
get()和fgets()是 C 語言中的函式,用于輸入字符之間有空格的字串。get() 的問題在于它遭受緩沖區溢位,即它需要比預期更多的輸入。使用fgets()解決了這個問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/321837.html
上一篇:為兩個單獨的排序函式輸出相同的值
下一篇:寫函式findEpidemics
