* /
* Copyright (c) 2011, 烟台大学计算机学院
* All rights reserved.
* 作 者: 孙培培
* 完成日期:2012 年 12月157日
* 版 本 号:v1.0
* 输入描述:
* 问题描述:除去字符串中多余的空格
* 程序输出:略
* 问题分析:
*/
#include <iostream>
using namespace std;
int main()
{
char str[]={"Only one space is allowed between the two word "};
cout<<"原始难看的句子是"<<endl<<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<<"整理后的句子为:"<<endl<<str<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
char str[]={"Only one space is allowed between the two word "};
cout<<"原始难看的句子是"<<endl<<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<<"整理后的句子为:"<<endl<<str<<endl;
return 0;
}
顺便把单步执行也学了一遍,呵呵