scanf("%c", a+i);
did not work because it consumes the newline left in the stdin
buffer in the previous scanf
call. The %c
conversion specifier in the format string of scanf
matches a character and the usual skip of leading white space is suppressed. When you add a leading space in the format string of scanf
scanf(" %c", a + i);
// ^ note the leading space
means scanf
will read and discard any number of leading whitespace characters. Thus it works in your case.