哪位大神帮忙C++环境下,编写一个将输入复制到输出的程序,并将其中的多个空格用一个空格代替。
我还没开始学C++,所以只能给你一个C的让你参考了。
#include
int main()
{
int c,i;
i=0;
while((c=getchar())!=EOF){
(c!=' ')?((i==0)?putchar(c):putchar(c),i=0):((i==0)?(putchar(c),i++):i++);
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
void main()
{
cout<<"Please enter a word:"<<endl;
istreambuf_iterator<char> beg(cin),end;
if(cin.fail())
{
cout<<"Error!"<<endl;
}
else
{
string str(beg,end);
size_t len=0;
for(string::iterator sbeg=str.begin();sbeg!=str.end();++sbeg)
{
if(*sbeg==' ')
{
for(string::iterator tmp=sbeg;*tmp==' ';++tmp,++len);
str.erase(sbeg,len+sbeg);
str.insert(sbeg,' ');
len=0;
}
}
cout<<"The result is:"<<endl<<str<<endl;
}
system("pause");
}