的話如何解決這個問題? https://code.google.com/codejam/contest/351101/dashboard#s=p1?反向C++中的句子,要求谷歌代碼果醬
我結束了的代碼如下,但它只能反向串了一個空間,因爲它是代碼保持字面邏輯考慮,反向整個字符串,扭轉的話,和完成。空間稍微混亂了,當我嘗試一個循環來檢測空間數量並據此採取行動時,它失敗了。請幫忙!代碼:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char revwrd[100];
char revstr[100];
string str;
getline(cin, str);
cout<<str;
int sps[10];
int len,y=0;
len = str.length();
cout<<"\n"<<"The Length of the string is:"<<len;
for(int x=len-1;x>-1;x--)
{
revstr[x] = str[y];
y++;
}
cout<<"\n"<<"The inverse of the string is:"<<"\n";
for(int z = 0;z<len;z++)
{
cout<<revstr[z];
}
cout<<"\n";
int no=0;
int spaces=0;
for(int a=0;a<len;a++)
{
if(revstr[a]== ' ')
{
sps[no]=a;
no++;
spaces++;
}
}
int rinc=0;
int spinc;
cout<<"\n";
spinc=sps[0];
int spinc2 = sps[0]+1;
int lend;
for(rinc=0;rinc<sps[0]+1;rinc++)
{
revwrd[rinc] = revstr[spinc];
spinc--;
}
for(lend=len;lend>sps[0];lend--)
{
revwrd[spinc2] = revstr[lend];
spinc2++;
}
cout<<"Spaces in the string:"<<spaces<<"\n";
cout<<"The words inversed are:"<<"\n";
for(int inc=1;inc<len+1;inc++)
{
cout<<revwrd[inc];
}
return 0;
}
爲什麼你會扭轉整條生產線,然後再重新逆轉的話嗎?在問題中給出的約束條件下,你可以** 1得到該行並將其放入'stringstream'中,** 2 **讀取該行中的每個單詞(只需使用'operator >>(ostream&,string& )'並將它壓入堆棧,** 3 **彈出堆棧中的每個單詞並將其打印到結果中。 (可能有更快的方法,但是這樣做的工作,應該很容易理解。) –
可能有用的三件事:字符串流,向量和反向迭代器。 –
請幫忙!不是一個問題。你有真正的問題嗎? – svick