「 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;
}