The realization of trim in C language

Source: Internet
Author: User
Tags printf strtok trim

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.