如何使用 c++ 反转句子字符串中的每个单词,例如"I love computer science"

How do I reverse each word in a sentence string like "I love computer science" using c++

本文关键字:例如 单词 science computer love c++ 何使用 句子 字符串      更新时间:2023-10-16

我正在尝试反转我输入的句子中的每个单词。

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

输出 = I evol retupmoc ecneicS

但这是上面输入的实际输出:

输出 = ecneicS reupmoc 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
}

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

#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;
}

可以直接使用 std::reverse

#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;
}

输出

Welcome to Reverse-the-words
Enter your sentence, word, or phrase
I love science
Your phrase with all of the words reversed is
I evol ecneics

获取句子后,将句子中的每个单词放入新变量中,然后反转并打印出来。

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

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

使用标准库始终是一种很好的做法。以下是我可以想出的在保持连续空格的同时反转单词的方法:

#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;
}

也许有一种方法可以写得更优雅,但我想你会明白的。