#include /* declarations for printf, fgets, BUFSIZ */ #include /* declaration for strlen */ int main() { char input[BUFSIZ]; int lineNum = 1; printf("Enter lines of input; use Control-D to exit:\n"); while (fgets (input, BUFSIZ, stdin)) /* fgets returns NULL at EOF */ { int length = strlen(input); /* length of line read in */ if ( length == 0 ) continue; /* if empty, read next line */ /* If the last character in the string is a newline ('\n'), "remove" it * by replacing it with a null byte. (On Windows, the newline might * be preceded by a separate carriage return ('\r'.) */ if (input[length - 1] == '\n') input[--length] = '\0'; /* pre-decrement length; replace '\n' */ if (input[length - 1] == '\r') input[--length] = '\0'; /* pre-decrement length; replace '\r' */ /* Print some information about the line. */ printf("Line %d: Contents: %s.\n", lineNum, input); printf("\tThe length is %d, the first character is: %c\n", length, input[0]); lineNum++; } }