#include <stdio.h>
#define SIZE 80
void readSentence(char * buf, int length);
int main()
{
char buffer[SIZE];
printf("请输入一个英文句子:\n");
readSentence( buffer, SIZE );
printf("处理后:\n[%s]\n", buffer );
return 0;
}
void readSentence(char * buf, int length)
{
char temp[SIZE],*pB,*pT;
gets(buf);
pB=buf;pT=temp;
while(*pB==' '||*pB=='\t') // 当字符串等于空格 或制表符
pB++; // 过滤掉前导空格
while(*pB!='\0') // 当字符串没结束。
{
if(*pB!=' '&&*pB!='\t')// 如果中间 字符不等于空格 并且不等于制表符
{
*pT++=*pB++; // 把pB指针的内容 赋值给PT
continue; // 继续
}
while(*pB!='\0'&&(*pB==' '||*pB=='\t')) // 把连续出现的'\0' 或者'\t' 转换成赋值一个空格.
pB++; //
if(*pB!='\0') //
*pT++=' '; //
}
*pT='\0';
pB=buf;pT=temp; // 让pB的地址指回buf, 让temp的地址指回pT. while循环里是对指针进行操作,操作完后,把开始的指向指回。
//
while(*pT!='\0')
*pB++=*pT++;
*pB='\0';
return;
}