#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
int Remove(char str[])
{
if (NULL == str)
{
return 0;
}
//删除空格计数
int removeCnt = 0;
//扫描头
int i = 0;
//当前字符为'\n',new_line为真,初始为真,可去除开头的空格
bool newline = true;
while (str[i])
{
str[i - removeCnt] = str[i];
//当前为空格
if (' ' == str[i])
{
//新行后的空格,应该删除
if (newline)
{
++removeCnt;
}
//空格后的空格,应该删除
else if (' ' == str[i + 1])
{
++removeCnt;
}
//换行前的空格,应该删除
else if ('\n' == str[i + 1])
{
++removeCnt;
}
//结尾的空格,应该删除
else if (0 == str[i + 1])
{
++removeCnt;
}
}
else if ('\n' == str[i])
{
newline = true;
}
else
{
newline = false;
}
++i;
}
str[i - removeCnt] = 0;
return removeCnt;
}
void main()
{
char s[] = "abc \n b d ";
printf("%d\n",Remove(s));
puts(s);
}