项目开发中str两头堵模型

1、题目要求:

去除字符串前后的空格字符。

#define  _CRT_SECURE_NO_WARNINGS 
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>

int getnewStr(char* src, char* dst)
{
	int ret = 0;
	char* p1 = src;
	int i = 0;
	int j = strlen(p1) - 1;
	int len = 0;
	while (isspace(p1[i]) && *(p1 + i) != '\0')
	{
		i++;
	}
	while (isspace(p1[j]) && p1[j] != '\0')
	{
		j--;
	}
	len = j - i + 1;
	strncpy(dst, p1 + i, len);
	return ret;
}

int main()
{
	char buf[] = "   abcd    ";
	char str[16] = { 0 };
	getnewStr(buf, str);
	printf("str=%s\n", str);
	system("pause");
	return 0;
}
发布了140 篇原创文章 · 获赞 26 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_41211961/article/details/104229519