He is an honest, friendly and hard-working student.
(a)將其存於一個字元陣列(char array)或字串(string, pointer to char)中,並利用循環語句,移動指標(pointer)找出逗點 “,” 的位置並輸出,如本例是在第16個字母位置,應輸出16。若輸入句中有一個以上之逗點,則所有位置一併輸出;若無逗點,則輸出 “NOT FOUND”。
(b)將其各個word(空白space所分開之字串)存於一個指標陣列(pointer array)中,再將其排序(sort)後列印出,如本例由小到大排序應輸出:
He an and friendly hard-working honest, is student.
2006-12-21 08:22:11 · 1 個解答 · 發問者 Anonymous in 電腦與網際網路 ➔ 程式設計
先給
#include
#include
main()
{
int i, j, k, a;
char ch, buf[80], *(ptr[80]);
printf("請輸入句子 (80字以內) => ");
gets(buf);
k=0; j = -1;
for (i=0;i
if (buf[i] == ',')
printf(", is in %d\n", i+1);
if (buf[i] == ' ')
{
a = i - j;
ptr[k] = malloc(a);
strncpy(ptr[k], &(buf[j+1]), a-1);
k++;
j = i;
}
}
a = i - j;
ptr[k] = malloc(a);
strncpy(ptr[k], &(buf[j+1]), a-1);
k++;
for (i=0;i
for (j=i+1;j
{
a=ptr[i];
ptr[i]=ptr[j];
ptr[j]=a;
}
}
for (i=0;i
}
2006-12-21 09:26:16 · answer #1 · answered by JJ 7 · 0⤊ 0⤋