如何反转字符串(逐字逐句)? [英] How to reverse a string(word by word), in place ?

查看:104
本文介绍了如何反转字符串(逐字逐句)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







我需要逐字翻译字符串。



逐字逐字地反转字符串。



例如

如果我们的字符串是房子是蓝色的

的返回值是blue is house the



这些单词是相反的,但字母仍然是有序的(在单词内)。



现在的问题是如何才能完成?



谢谢

解决方案

这是一个需要一个概念的概念最少的临时存储,虽然它的效率很低(如果你想要有效率,你就不会尝试逆转开始!):



1.开始编写一个在字符串中交换两个相邻单词的函数。注意副本的目标位置与源重叠的情况!

2.继续执行一个函数,该函数实现将两个任意字交换为一系列相邻字交换。

3.创建一个函数,确定需要交换的单词对。

4.然后编写一个函数,根据单个单词交换执行所需的反转。


你有很多指向 C C喜欢解决方案的链接,这是一个有效的 C ++ ,当然不适合你作业,以后可能会有所帮助:

  //   ReverseWords。 cpp:定义控制台应用程序的入口点。 

#include < span class =code-keyword>< string >
#includ e < sstream >
#include < vector >
#include < 算法 >
#include < iterator > ;
#include < ; iostream >

使用 命名空间标准;

void ReverseWords( char * str)
{
vector< string>话;
istringstream iss(str);
copy(istream_iterator< string>(iss),istream_iterator< string>(),back_inserter(words));
ostringstream oss;
copy(words.rbegin(),words.rend(),ostream_iterator< string>(oss, ));
copy_n(& oss.str()。front(),oss.str()。length() - 1 ,str);
}

int main()
{
char str [] = 房子是蓝色的;
cout<< str<< ENDL;
ReverseWords(str);
cout<< str<< ENDL;
return 0 ;
}



欢呼,

AR


我们正在为你做功课,我们正在帮助别人。你真的很想自己做功课。你有一个改变,以发展一些技能,不要放松它,它会在不久的将来得到回报。



如果你遇到困难,可以制定你的问题清楚地提供一个小的,自洽的代码示例,展示您的问题我们很乐意帮助您。



祝你好运,

-SA

Hi,


I need to reverse the string "word by word" .

Reverse the string word by word, in place.

for example
if our string is "the house is blue",
the return value would be "blue is house the".

The words are reversed, but the letters are still in order (within the word).

Now the problem is how could it be done?

Thanks

解决方案

Here's one concept that requires a minimum of temporary storing, although it's hopelessly inefficient (if you wanted to be efficient you wouldn't try in place reversal to start with!):

1. Start by writing a function that swaps two neighbouring words in a string. Watch out for cases where the target location of a copy overlaps the source!
2. Continue with a function that implements swaping two arbitrary words as a series of neighbouring word swaps.
3. Create a function that determines the pairs of words that you need to swap.
4. Then write a function that does the required reversal in terms of individual word swaps.


You got lots of links to C or C like solutions, this is a working C++ one, certainly not fit to your homework, maybe helpful later:

// ReverseWords.cpp : Defines the entry point for the console application.

#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>

using namespace std;

void ReverseWords(char* str)
{
    vector<string> words;
    istringstream iss(str);
    copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(words));
    ostringstream oss;
    copy(words.rbegin(), words.rend(), ostream_iterator<string>(oss, " "));
    copy_n(&oss.str().front(), oss.str().length() - 1, str);
}

int main()
{
    char str[] = "the house is blue";
    cout << str << endl;
    ReverseWords(str);
    cout << str << endl;
    return 0;
}


cheers,
AR


We're doing your homework for you, we're helping people. You're really interested in doing your homework by yourself. You're given a change to develop some skills, don't loose it, it will pay off in near future.

If you get stuck and can formulate your problem clearly and provide a small and self-consistent code sample demonstrating your problem we will gladly help you.

Good luck,
—SA


这篇关于如何反转字符串(逐字逐句)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆