Programming Example - Strings

#include <stdio.h>

int
main(int argc, char *args[]) {
    int max_line = 80;
    char line[80];
    int length;
    
    while (1) {
        printf("Enter a string: ");
        if (fgets(line, max_line, stdin) == NULL)
            break;
        
        /* should use strlen here */
        for (length = 0; line[length] != '\0'; length++)
            ;
        
        if (length <= 1)
            continue;
            
        if (line[length - 1]  != '\n') {
            printf("Line too long - limit %d characters.\n", max_line - 2);
            continue;
        }
        
        printf("This line is %d characters long.\n", length - 1);
        printf("Its first character is '%c'.\n", line[0]);
        printf("Its last character is '%c'.\n", line[length - 2]);
    }
    return 0;
}

Index