-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f6a85a
commit 2a35c97
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <gtest/gtest.h> | ||
|
||
class Solution { | ||
public: | ||
static auto reverse_words(std::string &word) -> std::string { | ||
std::stringstream ss(word); | ||
std::string result; | ||
|
||
while (ss >> word) { | ||
std::string reversed_word(word.rbegin(), word.rend()); | ||
result += reversed_word + " "; | ||
} | ||
|
||
if (!result.empty()) result.pop_back(); | ||
|
||
return result; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include "../include/solution.hpp" | ||
|
||
class ReverseWordsInAStringIII : public ::testing::Test { | ||
protected: | ||
~ReverseWordsInAStringIII() override = default; | ||
}; | ||
|
||
TEST_F(ReverseWordsInAStringIII, FirstTest) { | ||
std::string word = "Let's take LeetCode contest"; | ||
|
||
std::string result = Solution::reverse_words(word); | ||
std::string expected = "s'teL ekat edoCteeL tsetnoc"; | ||
|
||
ASSERT_EQ(result, expected); | ||
} | ||
|
||
TEST_F(ReverseWordsInAStringIII, SecondTest) { | ||
std::string word = "God Ding"; | ||
|
||
std::string result = Solution::reverse_words(word); | ||
std::string expected = "doG gniD"; | ||
|
||
ASSERT_EQ(result, expected); | ||
} | ||
|
||
auto main(int argc, char **argv) -> int { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |