1. 程式人生 > >去除字符串前後空格,挖出新字符串

去除字符串前後空格,挖出新字符串

log strlen blog str stdio.h har -i new gpo

#include<stdio.h>
void trimspae(char *str,char *newstr)
{
char*p=str;
int i,j,ncount;
i=0;
j=strlen(p)-1;
if(str==NULL||newstr==NULL)
{
printf("func trimspace() \n");
return -1;
}

while(isspace(p[i])&&p[i]!=0)
{
i++;
}
while(isspace(p[j])&&p[j]!=0)
{
j--;
}
ncount=j-i+1;
strncpy(newstr,str+i,ncount);
}

void main()
{
char *str=" qabfe ";
char buf[64]={0};
trimspae(str,buf);
printf("buf:%s",buf);
}

去除字符串前後空格,挖出新字符串