How to remove trailing (rtrim()) blanks, newlines and tabs from a string in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
  
#define LEN 30

void my_rtrim(char s[]);
  
int main(void)
{
    char s[LEN] = "abcd   \n   \t  ";
      
    my_rtrim(s);
    printf("s = %s\n", s);
     
    return 0;
}
 
void my_rtrim(char s[]) 
{ 
    int i;
    
    for (i = strlen(s) - 1; i >= 0; i--) 
         if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t') 
             break; 
    s[i + 1] = '\0'; 
} 
  

/*
run:
    
s = abcd
 
*/

 



answered Nov 11, 2015 by avibootz
edited Feb 21 by avibootz
...