给定字符串str形式的句子,任务是在 C++ 中反转给定句子的每个单词。
例子:
Input: str = “the sky is blue”
Output: blue is sky the
Inpu: str = “I love programming”
Output: programming love I
方法 1:使用STL 函数
- 使用 STL函数reverse() 反转给定的字符串str 。
- 迭代反转的字符串,只要找到空格,就使用 STL函数reverse() 反转该空格之前的单词。
下面是上述方法的实现:
CPP
// C++ program for the above approach
#include
using namespace std;
// Function to reverse the given string
string reverseString(string str)
{
// Reverse str using inbuilt function
reverse(str.begin(), str.end());
// Add space at the end so that the
// last word is also reversed
str.insert(str.end(), ' ');
int n = str.length();
int j = 0;
// Find spaces and reverse all words
// before that
for (int i = 0; i < n; i++) {
// If a space is encountered
if (str[i] == ' ') {
reverse(str.begin() + j,
str.begin() + i);
// Update the starting index
// for next word to reverse
j = i + 1;
}
}
// Remove spaces from the end of the
// word that we appended
str.pop_back();
// Return the reversed string
return str;
}
// Driver code
int main()
{
string str = "I like this code";
// Function call
string rev = reverseString(str);
// Print the reversed string
cout << rev;
return 0;
}
CPP
// C++ program for the above approach
#include
using namespace std;
// Function used to reverse a string
// from index l to r
void reversed(string& s, int l, int r)
{
while (l < r) {
// Swap characters at l and r
swap(s[l], s[r]);
l++;
r--;
}
}
// Function to reverse the given string
string reverseString(string str)
{
// Add space at the end so that the
// last word is also reversed
str.insert(str.end(), ' ');
int n = str.length();
int j = 0;
// Find spaces and reverse all words
// before that
for (int i = 0; i < n; i++) {
// If a space is encountered
if (str[i] == ' ') {
// Function call to our custom
// reverse function()
reversed(str, j, i - 1);
// Update the starting index
// for next word to reverse
j = i + 1;
}
}
// Remove spaces from the end of the
// word that we appended
str.pop_back();
// Reverse the whole string
reversed(str, 0, str.length() - 1);
// Return the reversed string
return str;
}
// Driver code
int main()
{
string str = "I like this code";
// Function call
string rev = reverseString(str);
// Print the reversed string
cout << rev;
return 0;
}
输出:
code this like I
方法 2:不使用内置函数:我们可以创建用于反转给定字符串的 reverse()函数。以下是步骤:
- 最初反转给定字符串str 的每个单词。
- 现在反转整个字符串以获得所需顺序的结果字符串。
下面是上述方法的实现:
CPP
// C++ program for the above approach
#include
using namespace std;
// Function used to reverse a string
// from index l to r
void reversed(string& s, int l, int r)
{
while (l < r) {
// Swap characters at l and r
swap(s[l], s[r]);
l++;
r--;
}
}
// Function to reverse the given string
string reverseString(string str)
{
// Add space at the end so that the
// last word is also reversed
str.insert(str.end(), ' ');
int n = str.length();
int j = 0;
// Find spaces and reverse all words
// before that
for (int i = 0; i < n; i++) {
// If a space is encountered
if (str[i] == ' ') {
// Function call to our custom
// reverse function()
reversed(str, j, i - 1);
// Update the starting index
// for next word to reverse
j = i + 1;
}
}
// Remove spaces from the end of the
// word that we appended
str.pop_back();
// Reverse the whole string
reversed(str, 0, str.length() - 1);
// Return the reversed string
return str;
}
// Driver code
int main()
{
string str = "I like this code";
// Function call
string rev = reverseString(str);
// Print the reversed string
cout << rev;
return 0;
}
输出:
code this like I
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解语言和 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程。