C语言实现去除空格算法


#include "stdio.h"
#include "stdlib.h"
#include "string.h"


int trimSpace(char *inbuf, char *outbuf)
{
char *in = inbuf;
char *out = outbuf;
int ret = 0;
int inLen = strlen(in);
if (!inbuf || !outbuf)
{
ret = -1;
printf("func trimSpace(char *inbuf, char *outbuf) err: ret = %d->function parameter is null.\n", ret);
return ret;
}
else
{
ret = 1;
for (int i = 0; i < inLen; i++)
{
if (in[i] != ' ')
{
*out++ = in[i];
}
}
}
return ret;
}


int main()
{
int vt = 0;
char str[100] = "hello world !";
char outStr[100] = { 0 };
printf("1:%s\n", str);
vt = trimSpace(str, outStr);
if (vt)
{
printf("2:%s\n", outStr);
}
system("pause");
return 0;
}


你可能感兴趣的:(C语言)