我想编写一个程序,它可以:当我输入“ Alan Turing ”时,它会输出“ Turing, A ”。但是对于我下面的程序,它输出“ uring, A ”,我想了很久但没弄清楚T去了哪里。这是代码:
#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;
}