如何使用c ++反转句子字符串中的每个单词,如“我爱计算机科学”

时间:2017-11-14 04:18:32

标签: c++

我试图反转我输入的句子中的每个单词。

例如: 输入=我喜欢计算机科学

输出= I evol retupmoc ecneicS

但这是通过上面的输入实际输出的内容:

输出= ecneicS retupmoc evol I

我不知道如何解决它请帮忙。谢谢你。

#include <iostream>
#include <string>
using namespace std;

// Prototype
void swap (char&, char&);
int main()
{

// Vars
string sentence;

// Input
cout << "Welcome to Reverse-the-words" << endl;
cout << "Enter your sentence, word, or phrase" << endl;
getline(cin,sentence);


     for (int i=0; i < sentence.length()/2; i++)
     {
        swap (sentence [i], sentence [sentence.length()-i-1]);
     }

// Output
cout << "Your phrase with all of the words reversed is " << endl;
cout << sentence << endl ;

return 0;
}

void swap(char &v1, char &v2)
{
    char temp;
    temp = v1;
    v1= v2;
    v2=temp
}

4 个答案:

答案 0 :(得分:1)

更新您的代码以反转句子中的每个单词:

#include <iostream>
#include <string>
using namespace std;

// Prototype
void swap (char&, char&);
int main()
{

// Vars
string sentence;

// Input
cout << "Welcome to Reverse-the-words" << endl;
cout << "Enter your sentence, word, or phrase" << endl;
getline(cin,sentence);

     int word_begin = 0;
     for (int i=0; i <=sentence.length(); i++)
     {  

         if (!isalpha(sentence[i]))
         {   

             for (int j = word_begin; j < (i - (i - word_begin)/2); j++)
             {
                 swap(sentence[j], sentence[i - j + word_begin - 1]);
             }

             word_begin = i + 1;
         }
     }

// Output
cout << "Your phrase with all of the words reversed is " << endl;
cout << sentence << endl ;

return 0;
}

void swap(char &v1, char &v2)
{
    char temp;
    temp = v1;
    v1= v2;
    v2 = temp;
}

答案 1 :(得分:0)

您可以直接使用std :: reverse

enum CarType {
    TOYOTA, NISSAN, UNKNOWN;

    public static CarType getByName(String name) {
        return Stream.of(CarType.values())
          .filter(x -> x.name().equals(name))
          .findFirst()
          .orElse(UNKNOWN);
    }
}

输出

#include <iostream>
#include <string>
#include <sstream>
#include<algorithm>
using namespace std;

int main()
{
    // Vars
    string sentence;

    // Input
    cout << "Welcome to Reverse-the-words" << endl;
    cout << "Enter your sentence, word, or phrase" << endl;
    getline(cin, sentence);

    istringstream iss(sentence);
    string word;
    cout << "Your phrase with all of the words reversed is " << endl;
    while (iss >> word) {
        std::reverse(word.begin(), word.end());
        cout<<word.c_str()<<" ";
    }
    return 0;
}

答案 2 :(得分:0)

获得句子后,将句子中的每个单词都变成一个新变量,然后反转并打印出来。

提示:在for循环中需要一个for循环。

for word in sentence:
    for character in word:
        /* your code to reverse the word */
    /* print the word */

答案 3 :(得分:0)

使用标准库始终是一种好习惯。这是我可以提出的在保持连续空格的同时反转单词的原因:

#include <string>
#include <algorithm>

std::string& reverse_words(std::string& sentence) {
  auto first = sentence.find_first_not_of(' ');
  auto last  = sentence.find_first_of(' ', first);
  while (last < std::string::npos) {
    std::reverse(&sentence[first], &sentence[last]);
    first = sentence.find_first_not_of(' ', last);
    last  = sentence.find_first_of(' ', first);
  }
  std::reverse(&sentence[first], &sentence[sentence.size()]);
  return sentence;
}

有一种方法可以更优雅地写出来,但我想你会明白这一点。