#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;
}