【问题标题】:Reverse words in a string c++ [duplicate]反转字符串c ++中的单词[重复]
【发布时间】:2014-07-20 12:04:56
【问题描述】:

我刚刚开始学习 C++。我正在编写一个程序来反转字符串中单词的顺序。如果有一句话,“我爱纽约!”。应该改为“!York New love I”。

我正在使用具有两个简单步骤的算法。

  1. 反转字符串。
  2. 颠倒单词的字母。

例如,对于上面的字符串,我将首先将其转换为“!kroY weN evol I”,然后将“!kroY”等单词的字母更改为“York!”。

现在的问题是我怎么知道这个词从哪里开始和从哪里结束。这就是我到目前为止所做的。但是这个程序没有按预期工作。我无法识别这个词,然后将其反转。

#include <iostream>
#include <string>

std::string reverseText(std::string x){
std::string y;
for(int i=x.size()-1;i>=0;i--) y += x[i];
return y;
}

std::string reverseWords(std::string x){

std::string y = reverseText(x);
bool wordFound = true;
std::string temp1,ans;

for(size_t i=0;i<y.size();i++){


    if(wordFound){ 

        if(y[i]!=' ') temp1+=y[i];  // if there is a letter, store that in temp1.

        else if(y[i]==' ')   // if there is a space, that means word has ended.
        {
            ans += reverseText(temp1);  // store that word, in ans.
            temp1=" ";                  
            wordFound=false;}
        }

    if(y[i]==' ' && y[i+1]!=' ') wordFound=true;
    }
return ans;
}

int main(){
std::cout<<reverseWords("My name is Michael");
}

输出:Michaelis 名字

【问题讨论】:

  • 什么不起作用?运行时会发生什么?
  • @Theolodis 输出不正确,我的逻辑不对。我猜。
  • 好吧,将你得到的输出添加到问题中会很好;)
  • @Theolodis 谢谢,我已经添加了输出。
  • 该问题现已作为重复项关闭,但其他问题中的所有答案都是错误的。 Here 是我的。

标签: c++ string algorithm


【解决方案1】:

我没有对此进行广泛的测试,它仍然可能存在问题,但它为您提供的案例产生了正确的输出。我试图在不做太多改动的情况下修复你的代码。

#include <iostream>
#include <string>

std::string reverseText(std::string x){
    std::string y;
    for(int i=x.size()-1;i>=0;i--) y += x[i];
    return y;
}

std::string reverseWords(std::string x) {
    std::string y = reverseText(x);
    bool wordFound = true;
    std::string temp1 = " ", ans;

    for(size_t i = 0; i < y.size(); i++) {
        if(wordFound){
            if(y[i] != ' '){
                temp1 += y[i];  // if there is a letter, store that in temp1.
            } else if(y[i]==' ') {  // if there is a space, that means word has ended.
                ans += reverseText(temp1);  // store that word, in ans.
                temp1 = " ";
                wordFound=false;
            }
        }
        if(y[i]==' ' && y[i+1]!=' ') wordFound=true;
    }
    ans += reverseText(temp1);
    return ans;
}

int main(){
    std::cout<<reverseWords("My name is Michael");
}

变更摘要

你忘了用空格初始化第一个字符串

std::string temp1 = " ", ans;

循环完 y 后,您忘记将 temp1 的内容“刷新”到 answer 中

ans += reverseText(temp1);

【讨论】:

  • 谢谢。它适用于这种情况。但如果用户写道,“我爱纽约!”。它返回,“York!新的爱我”。但正如我在问题中提到的,它应该是“!York new love I”。您能提出解决此问题的方法吗?
  • @user3834119:你需要改变你定义单词的方式。在您的代码中,您将其定义为“以空格分隔的字符串”,!York 不以空格分隔,因此被视为一个单词。
  • @MatthieuM。你说的对。但我想不出一种方法来定义除 this(空格)以外的单词。
  • @user3834119:嗯,最简单的解决方案是使用集合。它可以是一组“单词组成”字符或一组“单词边界”字符。例如,假设您只处理 ASCII 字符串,您可以说组成单词的字符是[A-Za-z-'],而其他任何字符都是单词边界。然后,如果您查看规范问题的second answer,只需将测试in_text[rindex] == ' ' 替换为is_word_boundary(in_text[rindex])
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多