Remove trailing blanks and tabs from each line of input, and to delete entirely blank lines. - C String

C examples for String:char array

Description

Remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.

Demo Code

#include <stdio.h>

#define MAXLINE 1000/*from   www  .j  a v  a2s  .c o m*/

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 == 1 && line[0] == '\n')
            continue;
        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' && l < lim - 1)
        s[l++] = c;
    s[l] = '\0';

    return l;
}

Result


Related Tutorials