发新话题
打印

C语言中trim的实现

本主题由 大道至简 于 2008-6-26 00:30 移动

C语言中trim的实现

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

char seps[]   = " ";

char* trim(char* desc,char* src,char* seps);
int main(int argc, char* argv[])
{
char szResult[1024]="";
memset(szResult,0,1024);

char strtemp[]="ab c d e f";

printf( "%s\n\nTokens:\n", strtemp );
trim(szResult,strtemp,seps);
    printf( "result:%s(ok!)\n", szResult );
return 0;
}
//////////////////////////////////////////////////////
char* trim(char* desc,char* src,char* seps)
{
char* token=NULL;
  /* Establish string and get the first token: */
   token = strtok(src, seps);
   while( token != NULL )
   {
      /* While there are tokens in "string" */
      printf( " %s\n", token );
  strcat(desc,token);
      /* Get next token: */
      token = strtok( NULL, seps );
   }
   return desc;
}
因为梦想而努力,因为有你而精彩!
mail: qianzongming@gmail.com

TOP

说明

1.seps是需要去除的字符数组,可以有几个字符,也可以一个。这里是空格,最常用的。
2.参数也很简单,第一个是结果数组指针,第二个是原字符数组指针,第三个是需要去掉的字符数组指针。返回的是结果数组指针。
因为梦想而努力,因为有你而精彩!
mail: qianzongming@gmail.com

TOP

很不错的说

TOP

发新话题