如何创建倒排的句子和单词

How to create a backwards sentence and words?

本文关键字:句子 单词 何创建 创建      更新时间:2023-10-16

嘿,伙计们,我是新来的,是编程高手,请耐心等待。

这是给我的C++课的,遗憾的是,我的老师教得太差了,很多东西让我困惑,所以我需要一些帮助。

我们有一个名为"反向句子"的实验室,这就是它在这个实验室想要的。

编写函数";反转句子";它获取一个字符串参数并通过反转来更改它。

例如:

INPUT:第一次测试

输出:tset tsrif eht

函数不能使用额外的字符串,但必须反转输入字符串的元素。

#include <iostream>
#include <string>
using namespace std;
void ReverseSentence( string& inputSentence){
   /* Body of Function Here */
}
int main(){
   string inputSentence;
   cout << "Input your sentence: ";
   getline(cin, inputSentence);
   cout << endl;
   ReverseSentence(inputSentence);
   cout << "Reversed Sentence:" << endl;
   cout << inputSentence << endl;
   return 0;
}

有人能帮我什么功能吗,因为我有问题。

只需使用std::reverse:

void ReverseSentence( string& inputSentence){
  std::reverse(inputSentence.begin(), inputSentence.end());
}

循环和swap的一半。

#include<algorithm>
#include<string>
void ReverseSentence(std::string &s){
   for (int i = 0; i < s.size()/2; ++i)
      std::swap(s[i], s[s.size() - i - 1]);
}