[cpp]
/*
* Copyright (c) 2012, 煙台大學計算機學院
* All rights reserved.
* 文件名稱:test.cpp
* 作者:樊露露
* 完成日期:2012 年12月16日
* 版本號:v1.0
*
* 輸入描述:無
* 問題描述:出去字符串中多余的空格
* 程序輸出:整理好的句子
* 問題分析:
* 算法設計:略
*/
#include<iostream>
using namespace std;
int main(){
char str[]="Only one space is allowed between the two words.";
cout<<"原始難看的句子:"<<str<<endl;
int i=0,j=0;
bool notSpace;
while(str[j]==' ')
j++;
notSpace=true;
while(str[j]!='\0'){
if(str[j]!=' '){
notSpace=true;
str[i++]=str[j++];
}else if(notSpace){
notSpace=false;
str[i++]=str[j++];
}else{
j++;
}
}
str[i]='\0';
cout<<"整理後的句子:"<<str<<endl;
return 0;
}