Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Reverse words feature #5131

Merged
merged 5 commits into from
Jan 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <iostream>
#include <sstream>
#include <vector>

void reverseWords(std::string& str) {
std::istringstream iss(str);
std::vector<std::string> words;

// Split the string into words
while (iss >> str) {
words.push_back(str);
}

// Reverse the order of words
std::reverse(words.begin(), words.end());

// Construct the reversed string
str.clear();
for (const auto& word : words) {
str += word + " ";
}

// Remove the trailing space
if (!str.empty()) {
str.pop_back();
}
}

int main() {
std::string input;

// Input string
std::cout << "Enter a string: ";
std::getline(std::cin, input);

// Reverse the words in the string
reverseWords(input);

// Output the reversed string
std::cout << "Reversed string: " << input << std::endl;

return 0;
}