Trim trailing whitespace

    0

    0

    Giovanny Gongora

    Codiga's C Recipes

    Whitespace is defined by isspace(). Returns s, modified to be terminated at the start of its trailing whitespace.

    #include <string.h>
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    char *
    rtrim(char *s) {
    	char *p = s + strlen(s) - 1;
    	while (p >= s && isspace((unsigned char) *p)) {
    		*p = '\0';
    		p--;
    	}
    	return s;
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.