的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