#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void getstr(char *origstr, char *newstr/*out*/,int *count)
{
char *phead = origstr;
int i = 0;
int j = strlen(phead) - 1;
if (phead == NULL || newstr==NULL || count==NULL)
{
return;
}
while (phead[i] ==' '&& phead[i]!='\0')
{
i++;
}
while (phead[j] ==' '&& phead[j]!='\0')
{
j--;
}
*count = j - i + 1;
strncpy(newstr, phead + i, *count);
newstr[*count] = '\0';//赋值后,newstr后没有结束符,需手动加入
}
void main()
{
char *origstr=" abcd ";
//char *newstr=NULL ;//错误,指针所指内存空间不能被修够,即不能写入数据
char newstr[100] = { 0 };
int count=0;
getstr(origstr, newstr, &count);
printf("%s\n", newstr);
printf("%d\n", count);
}
两头堵模型:有一个字符串开头或结尾有n个空格,欲去掉前后空格返回一个新的字符串
最新推荐文章于 2020-09-11 08:43:27 发布