Going through K&R, I'm trying to get my head around C. I want to write a program that prints on the screen the user's previous line, unless the character was "a".
int main(){
int c;
while((c=getchar())!=EOF){
if(c!='a')
putchar(c);
}
return 0;
}
Yes, the program isn't much. But it won't work as intended. Do I need to use the ASCII value of the character "a", because the above code just prints all the letters regardless of being a or not.
The code should work as specified, but what you will find is that using getchar
with the while loop will print a carriage return when a is entered. This is because in the current implementation getchar
will keep reading the input buffer till it is empty, if you wanted to stop this happening you could flush it in the if statement.
Entering a string of text at the moment will print the string removing any *a*s