在Google Code jam上问的一个c++句子的倒序
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
中读取单个单词,直到它看到换行符(可能有更好的方法,但它可以工作)。
现在,为了反转每一行输入,显然需要在读取字符串时将它们存储在某个地方。最简单的方法是将它们存储到vector容器中:
std::vector< std::string > strings;
...
while ( stream >> word )
{
strings.push_back( word );
if ( stream.peek() == 'n' )
break;
}
现在你有了一个包含行中所有字符串的向量,你只需要将它们以相反的顺序打印出来。可以使用反向迭代器遍历vector:
std::vector< std::string >::reverse_iterator it;
for ( it = strings.rbegin(); it != strings.rend(); ++it )
{
std::cout << *it << " ";
}
std::cout << std::endl;
rbegin()
方法返回一个迭代器,该迭代器指向vector中最后一个元素;rend()
方法返回一个迭代器,该迭代器指向vector的第一个元素之前的元素;++it
将迭代器推进到vector对象的下一项,即返回vector对象的前一项;*it
给出迭代器所指向的字符串。您可以更深奥一点,使用copy
模板函数:
std::copy( strings.rbegin(),
strings.rend(),
std::ostream_iterator<std::string>( std::cout, " " )
);
这个方法调用取代了上面的循环。它创建一个新的ostream_iterator
,将字符串写入cout
,并用一个空格字符分隔。
对于这个特殊练习的条件,这已经足够了。如果你被要求保留空格,或者考虑标点符号或大写,那么你必须做一些更低级的事情。
只有几个循环和if:
// 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:现在从end
开始遍历这个整数数组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==' '){
//std::cout<<p1-p0<<' ';
for(i=0;i<p1-p0;++i) temp[i]=p0[i]; temp[i]=' ';
p0 = p1+1;
container.push_back(temp);
std::cout<<temp;
}
p1++;
}while(*(p1-1)!=' ');
std::cout<<std::endl;
for(int i=container.size()-1;i>=0;i--) std::cout<<container[i]<<' ';
return 0;
}
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 用C++中的一个变量定义一个常量
- 函数向量_指针有不同的原型,我可以构建一个吗
- 在c++中用vector填充一个简单的动态数组
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 预处理器:插入结构名称中的前一个行号
- 我在c++代码中生成了一个运行时#3异常
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 从链接列表c++中删除一个项目
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '