的getchar我想寫一個程序,它可以: 當我進入,說:「阿蘭·圖靈」時,輸出「圖靈,A」。 但對於我的後續程序,它輸出「uring,A」,我想了很久,但未能弄清楚T去哪裏。 下面是代碼:問題在C
#include <stdio.h>
int main(void)
{
char initial, ch;
//This program allows extra spaces before the first name and between first name and second name, and after the second name.
printf("enter name: ");
while((initial = getchar()) == ' ')
;
while((ch = getchar()) != ' ') //skip first name
;
while ((ch = getchar()) == ' ')
{
if (ch != ' ')
printf("%c", ch); //print the first letter of the last name
}
while((ch = getchar()) != ' ' && ch != '\n')
{
printf("%c", ch);
}
printf(", %c.\n", initial);
return 0;
}
偉大的解決方案!謝謝! – asunnysunday 2011-06-05 09:39:13