删除字符串中的多余空白,包含前,中,尾。
“ I am a boy . ”->"I am a boy"
/*
*删除多余的空白,包括前面,后面,中间
*" I am a boy . " => "I am a boy ."
*/
#include <stdio.h>
#include <string.h>
#define NUM_Max 1000
void back_space(char str[],char outstr[],int n,int len){
int space_num=0;
int i=0;
int j=0;
while (j<len)
{
if (str[j]==' ')
{
if(i==0){
++j;
}else
{
if (space_num!=0)
{
++j;
}else
{
outstr[i++]=' ';
++space_num;
++j;
}
}
}else{
space_num=0;
outstr[i++]=str[j];
j++;
}
}
}
int main(void){
char str[]=" I am a boy . ";
printf("%s
",str);
char outstr[NUM_Max];
back_space(str,outstr,NUM_Max,sizeof(str));
printf("%s
",outstr);
getchar();
return 0;
}