#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/*
有一个字符串开头或结尾含有n个空格(" hello "),欲去掉前后空格,返回一个新字符串。
要求1:请自己定义一个接口(函数),并实现功能。
要求2:编写测试用例。
*/
int trimspace(char* in, char* out)
{
int ret = 0;
if (in == NULL || out == NULL)
{
ret = -1;
printf("parameter null error!\n");
return ret;
}
char* p1 = NULL;
char* p2 = NULL;
int n = 0;
n = strlen(in);
p1 = in;
p2 = in + n - 1;
while (isspace(*p1))
{
p1++;
}
while (isspace(*p2))
{
p2--;
}
int len = p2 - p1 + 1;
strncpy(out, p1, len);
return ret;
}
int main()
{
int ret = 0;
char instr[64] = " hello world ";
char outstr[64] = { 0 };
ret = trimspace(instr, outstr);
printf("outstr=%s\n", outstr);
system("pause");
return ret;
}