如何创建倒排的句子和单词
How to create a backwards sentence and words?
嘿,伙计们,我是新来的,是编程高手,请耐心等待。
这是给我的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]);
}
相关文章:
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- 如何获取句子中单词的第一个字母*没有*指针,引用等
- 如何输入每个单词在字符串数组中的句子,直到用户按Enter键
- 用句子从单词打印单词
- 如何在文件中找到单词或句子
- 如何删除句子 c++ 中的整个单词
- 使用 getline 和字符串流检查句子中的单词是否在文件中
- 如何从包含多个句子的输入文件中表达单个单词.C
- 从输入的句子中构建单词列表
- C 如何在字符串中大写每个句子/行的第一个单词
- 动态分配的数组C 读取句子并打印单词
- 计数句子中单词的字符数
- 大写每个句子中的第一个单词
- 在 C++ 的句子中查找某个单词
- 句子中的单词顺序相反
- 如何使用 c++ 反转句子字符串中的每个单词,例如"I love computer science"
- 我如何将句子分成一个单词
- 如何在C++中迭代一个句子的单词
- 如何使用C 中的Regex在给定句子中的多个空格隔开的两个单词匹配
- C++字符[句子/单词]