This paper introduces the implementation of trim in C language in detail.
Describe
I used ATL to write a COM, does not support MFC, so can not use CString, but support C code, encountered string (character array), want to remove the space in the string, C, there is no trim function, find and did not find, a few lines of code themselves write it. In the future, it is convenient for everyone to meet.
Description
1.seps is an array of characters that need to be removed, and can have several characters or one. Here are the spaces, most commonly used.
2. The parameter is also very simple, the first is the result array pointer, the second is the original character array pointer, and the third is the character array pointer that needs to be removed. Returns the result array pointer.
#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 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;
}