C实现去空格的实例

来源:互联网 发布:新乡青峰网络 编辑:程序博客网 时间:2024/05/29 19:04
//去左空格char* ltrim(char *ptr){    int start,end,i;     end=strlen(ptr)-1;    if (ptr)      {          for(start=0; isspace(ptr[start]); start++)              ;          for(i=start; i<=end; i++)              ptr[i-start]=ptr[i];          ptr[end-start+1]='\0';          return (ptr);      }      else          return NULL; }//去右空格char* rtrim(char *ptr){    int start,end,i;     start=0;    if (ptr)      {          for(end=strlen(ptr)-1; isspace(ptr[end]); end--)             ;          for(i=start; i<=end; i++)              ptr[i-start]=ptr[i];          ptr[end-start+1]='\0';          return (ptr);      }      else          return NULL; }//去两边空格char * trim(char * ptr)  {      int start,end,i;      if (ptr)      {          for(start=0; isspace(ptr[start]); start++)              ;          for(end=strlen(ptr)-1; isspace(ptr[end]); end--)              ;          for(i=start; i<=end; i++)              ptr[i-start]=ptr[i];          ptr[end-start+1]='\0';          return (ptr);      }      else          return NULL;  }//去所有空格char* alltrim(char *dstr){    int i,j = 0;    char tmp[4096] = {0};    if (dstr)      {        strcpy(tmp,dstr);        for (i=0;i<strlen(tmp);i++)        {                        if (!isspace(tmp[i])&&tmp[i]!=NULL)            {                dstr[j] = tmp[i];                    j++;                            }                    }        dstr[j] = '\0';        return (dstr);    }else{        return NULL;    }}

 

0 0
原创粉丝点击