Here is program to read character from the console and print them in a reverse order.
#include<stdio.h>
main()
{
int ch, count = 0;
char a[100];
printf("Enter Charachters\n");
ch = getchar();
while(ch != EOF && count<100)
{
a[count] = ch;
count = count+1;
ch = getchar();
}
printf("\ncount = %d\n",count);
while (count>0)
{
count = count -1 ;
putchar(a[count]);
}
}
My questinon is: When we give EOF character (ctrl+D) after typing in a few characters on the console, it does not exit out of the loop. It does not add to the count variable but also does not exit the loop. Only if the EOF character is the first character after a newline character, it is read properly and the loop is exited. For eg if the sample input is:
abcdef
abc
ctrl+D
Then the code works fine but if the input is:
abcdef ctrl+D
The loop is not exited.
Tell me a way to accomplish this.
Thanks