Remove trailing blanks and tabs - C String

C examples for String:String Function

Description

Remove trailing blanks and tabs

Demo Code

#include <stdio.h>
#include <string.h>

#define MAXLINE 1000/*from   w w w .jav  a2 s. c  o  m*/

/* trim:  remove trailing blanks, tabs, newlines */
int trim(char s[])
{
    int n;

    for (n = strlen(s)-1; n >= 0; n--)
        if (s[n] != ' ' && s[n] != '\t' && s[n] != '\n')
            break;
    s[n+1] = '\0';
    return n;
}

int main()  /* trim:  remove trailing blanks and tabs */
{
    int n;
    char line[MAXLINE] = "test   ";

    trim(line);
    printf("%s<\n", line);
    
    return 0;
}

Result


Related Tutorials