Write a program to print all input lines that are longer than 80 characters. - C String

C examples for String:char array

Description

Write a program to print all input lines that are longer than 80 characters.

Demo Code

#include <stdio.h>

#define MAXLINE 1000// w w  w .  ja va  2  s . c  om
#define MAXLENGTH 10

int get_line(char line[], int maxline);

int main(void){
    int len; /* current line length */
    char line[MAXLINE]; /* current input line */

    while ((len = get_line(line, MAXLINE)) > 0){
        if (len > MAXLENGTH)
            printf("%s\n", line);
    }
    return 0;
}
int get_line(char s[], int lim){
    int c, i, l;

    for (i = 0, l = 0; (c = getchar()) != EOF && c != '\n'; ++i) {
        if (i < lim - 1)
            s[l++] = c;
    }
    if (c == '\n')
        if (l < lim - 1)
            s[l++] = c;
    s[l] = '\0';

    return l;
}

Result


Related Tutorials