#include <iostream>
#include <vector>
#include <string>
#include <regex>
// Didn't work with Unicode
// Returns the word before the last word in a string.
// Handles punctuation, multiple spaces, tabs, and other edge cases.
// NOTE: This version treats "words" as sequences of A–Z or a–z letters.
// Full Unicode word support would require a different library (e.g., ICU, Boost.Regex).
std::string getWordBeforeLast(const std::string& text) {
// Empty or whitespace-only input → no result
if (text.find_first_not_of(" \t\n\r") == std::string::npos) {
return "";
}
// Extract words using a C++-compatible regex.
// This pattern matches ASCII letters only: A–Z and a–z.
std::regex wordPattern(R"([A-Za-z]+)");
std::sregex_iterator it(text.begin(), text.end(), wordPattern);
std::sregex_iterator end;
std::vector<std::string> words;
for (; it != end; ++it) {
words.push_back(it->str());
}
// Need at least two words to return the one before the last
if (words.size() < 2) {
return "";
}
// Return the second-to-last word
return words[words.size() - 2];
}
int main() {
std::cout << "=== Testing: Get Word Before Last ===\n\n";
// Test cases to validate all edge conditions
std::vector<std::string> testCases = {
"python c++",
" many spaces here now ",
"OneWord",
"",
" ",
"Hello, world!",
"Tabs\tand\nnewlines work too",
"Unicode 世界、こんにちは",
"Ends with punctuation.",
"Multiple words, with punctuation, here!",
"state-of-the-art program example"
};
for (const auto& test : testCases) {
std::string result = getWordBeforeLast(test);
std::cout << "Input: \"" << test << "\"\n";
std::cout << "Output: " << (result.empty() ? "null" : result) << "\n";
std::cout << std::string(40, '-') << "\n";
}
}
/*
OUTPUT:
=== Testing: Get Word Before Last ===
Input: "python c++"
Output: python
----------------------------------------
Input: " many spaces here now "
Output: here
----------------------------------------
Input: "OneWord"
Output: null
----------------------------------------
Input: ""
Output: null
----------------------------------------
Input: " "
Output: null
----------------------------------------
Input: "Hello, world!"
Output: Hello
----------------------------------------
Input: "Tabs and
newlines work too"
Output: work
----------------------------------------
Input: "Unicode 世界、こんにちは"
Output: null
----------------------------------------
Input: "Ends with punctuation."
Output: with
----------------------------------------
Input: "Multiple words, with punctuation, here!"
Output: punctuation
----------------------------------------
Input: "state-of-the-art program example"
Output: program
----------------------------------------
*/