#include #include #include #define MAX 10 // max char per line // The code handles all the edge cases nicely now. // - where the first character is newline // - where the first character is EOF // - where there is one character // - where there are more characters input than the max size of the array // - where characters are entered terminated by a newline // - where there are characters entered terminated by a EOF (control D) int main(void) { int i; char s[MAX]; for (i = 0; i < MAX-1; i++) { if ((s[i] = getchar()) == '\n' || s[i] == EOF) break; printf("s[%d]=%c\n", i, s[i]); } s[i] = '\0'; printf("string is %s\n", s); return 0; }