[英]Reverse words of a sentence in c++, Asked on Google Code jam
如何解决呢? https://code.google.com/codejam/contest/351101/dashboard#s=p1吗?
我最终得到的代码在下面,但是它只能将字符串反转最多一个空格,因为它的代码考虑了字面逻辑,反转整个字符串,反转单词并完成。 空格有些混乱,当我尝试执行循环以检测空格数并采取相应措施时,它失败了。 请帮忙! 码:
#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;
}
挑战的条件是单词之间只有一个空格,并且空格不会出现在行的开头或结尾,因此对于此特定练习,您不必担心保留空格。 只要您在每个单词之间用一个空格写输出,就可以了。
考虑到这一点,您可以使用常规格式的输入来阅读每个单词:
std::string word;
...
while ( stream >> word )
// do something with word
您不必担心缓冲区的大小,也不必担心检测空格等。您确实需要担心检测换行符,但是使用peek
方法很容易做到:
while ( stream >> word )
{
// do something with word;
if ( stream.peek() == '\n' )
break;
}
上述循环会从输入流中读取单个单词stream
,直到它看到一个换行符(可能有更好的方式来做到这一点,但它的作品)。
现在,为了反转输入的每一行,您显然需要在读取字符串时将它们存储在某处。 最简单的方法是将它们存储到向量中:
std::vector< std::string > strings;
...
while ( stream >> word )
{
strings.push_back( word );
if ( stream.peek() == '\n' )
break;
}
因此,现在您有了一个包含行中所有字符串的向量,只需要以相反的顺序将它们打印出来即可。 您可以使用反向迭代器遍历向量:
std::vector< std::string >::reverse_iterator it;
for ( it = strings.rbegin(); it != strings.rend(); ++it )
{
std::cout << *it << " ";
}
std::cout << std::endl;
rbegin()
方法返回一个迭代器,该迭代器指向向量中的最后一个元素。 rend()
方法返回一个迭代器,该迭代器指向向量的第一个元素之前的元素; ++it
使迭代器指向向量中的下一项,并回到最前面; *it
给出迭代器指向的字符串。 您可以获得更多深奥的知识,并使用copy
模板功能:
std::copy( strings.rbegin(),
strings.rend(),
std::ostream_iterator<std::string>( std::cout, " " )
);
单个方法调用替换了上面的循环。 它创建一个新的ostream_iterator
,它将字符串写入cout
,并用单个空格字符分隔。
对于此特定练习的条件,这已足够了。 如果需要保留间距,或者要考虑标点符号或大写字母,那么您就必须在底层进行一些操作。
仅有几个循环,如果是:
// Reverse Words
#include <iostream>
#include <string>
using namespace std;
int main() {
int tc; cin >> tc; cin.get();
for(int t = 0; t < tc; t++) {
string s, k; getline(cin, s);
for(int i = (s.length()- 1); i >= 0; i--) {
if(s[i] == ' ' || (i == 0)) {
if(i == 0) k += ' ';
for(int j = i; j < s.length(); j++) {
k += s[j];
if(s[j+1] == ' ' ) break;
}
}
}
cout << "Case #" << t + 1 << " " << k << endl;
}
return 0;
}
这可能处理多个空格:
std::string ReverseSentence(std::string in)
{
std::vector<string> words;
std::string temp = "";
bool isSpace = false;
for(int i=0; in.size(); i++)
{
if(in[i]!=' ')
{
if(isSpace)
{
words.push_back(temp);
temp = "";
isSpace = false;
}
temp+=in[i];
}
else
{
if(!isSpace)
{
words.push_back(temp);
temp = "";
isSpace = true;
}
temp += " ";
}
}
std::reverse(words.begin(),words.end());
std::string out = "";
for(int i=0; i<words.size(); i++)
{
out+=words[i];
}
return out;
}
您可以按照以下方法:
步骤1:只需检查输入数组中的空格,即可将它们的索引号存储在整数数组中。
步骤2:现在从结尾遍历此整数数组
step a : make a string by copying characters from this index to previous index .
note : since for first element there is no previous element in that case you will copy from this index to end of the input string .
step b : step a will give you a word from end of input string now add these word with a space to make your output string .
我希望这能帮到您 。
这个问题确实是递归的:
void reverse()
{
string str;
cin >> str;
if (cin.peek() != '\n' || cin.eof()) {
str = " " + str;
reverse();
}
cout << str;
}
int main(int argc, const char * argv[])
{
int count = 0;
cin >> count;
for (int i = 0; i < count; i++) {
cout << "Case #" << (i + 1) << ": ";
reverse();
cout << endl;
}
return 0;
}
因此,我逐字阅读并在单词前面添加一个空格,直到到达行尾或文件尾。 一旦到达行尾,递归将解开并以相反的顺序打印读取的字符串。
我追求蛮力,我非常想使用指针!
就是这个:
#include <iostream>
#include <string>
#include <vector>
int main()
{
char *s1 = new char[100];
std::cin.getline(s1, 100);
std::vector<std::string> container;
char* temp = new char[100];
char *p1, *p0;
p1 =p0 = s1;
int i;
do{
if (*p1==' ' || *p1=='\0'){
//std::cout<<p1-p0<<' ';
for(i=0;i<p1-p0;++i) temp[i]=p0[i]; temp[i]='\0';
p0 = p1+1;
container.push_back(temp);
std::cout<<temp;
}
p1++;
}while(*(p1-1)!='\0');
std::cout<<std::endl;
for(int i=container.size()-1;i>=0;i--) std::cout<<container[i]<<' ';
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.