/*===============================================
有一个字符串开头或结尾含有n个空格
" abcdefg "
欲去掉前后空格,返回一个新字符串。
要求1:请自己定义一个接口(函数),并实现功能;70分
要求2:编写测试用例。30分
===============================================*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
int trimSpace(char* inbuf, char* outbuf)
{
int ret = 0;
int ncount = 0;
char* p = inbuf;
if (inbuf == NULL)
{
ret = -1;
printf("func error:%d\n", ret);
return ret;
}
int i = 0;
int j = strlen(p) - 1;
while (isspace(p[i]) /*&& *(p + i) != '\0'*/)
{
i++;
}
while (isspace(p[j]) /*&& p[j] != '\0'*/)
{
j--;
}
ncount = j - i + 1;
strncpy(outbuf, p + i, ncount);
return ret;
}
int main()
{
int ret = 0;
char buf[] = " abcdefg ";//7
char newstr[16] = { 0 };
ret = trimSpace(buf, newstr);
if (ret != 0)
{
printf("func error\n");
}
printf("newstr=%s\n", newstr);
printf("new strlen=%d\n", strlen(newstr));
system("pause");
return 0;
}
删除字符串前后的空格20200209
猜你喜欢
转载自blog.csdn.net/weixin_41211961/article/details/104238288
今日推荐
周排行