I'm trying to read a string in C until EOF, but react to every character before the input of the next character.
For example:
A user inputs abcd
, and right after every character input, I will do some calculations on each character.
I have tried to approach this in two different ways:
Option 1, using getchar():
int d;
char c;
while ((d = getchar()) != EOF) {
c = (char)d;
<<< DO SOME CALCULATIONS ON 'c'>>>
}
Option 2, using scanf:
char c;
while (scanf(" %c",&c) != EOF) {
<<< DO SOME CALCULATIONS ON 'c'>>>
}
However, both approaches seem to fail. It seems that the getchar() and scanf() gets all the characters until it encounters the 'EOF', and only then enters the while loop.
Would love some help on this, Thanks!