removes trailing blanks, tabs, and newlines from string str: - C String

C examples for String:String Function

Description

removes trailing blanks, tabs, and newlines from string str:

int trim(char str[])
{
  int p;
  for(p = strlen(str) - 1; p >= 0; p--)
    if(str[p] != ' ' && str[p] != '\t' && str[p] != '\n')
       break;
  str[p + 1] = '\0';
  return p;
}

#include <stdio.h>

int trim(char str[])
{
  int p;
  for(p = strlen(str) - 1; p >= 0; p--)
    if(str[p] != ' ' && str[p] != '\t' && str[p] != '\n')
       break;
  str[p + 1] = '\0';
  return p;
}


int main(void)
{
   char *str = "  this is a  test   ";

   str = trim(str);

   puts(str);

   return 0;
}

Related Tutorials