项目开发中字符串模型1-去除空格

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值