我正在尝试解决 C++ 问题。即在包含全部大写字母的字符串中的单词之间添加空格。 我无法弄清楚其中的逻辑。
示例:
您正在寻找称为词汇数据库的东西,例如WordNet。
这是一个小例子:
#include <iostream>
#include <string>
#include <vector>
#include <wn.h> // WordNet library's header file
int main() {
std::string input = "ILOVEYOU"; // The string you are going to parse
std::vector<std::string> words; // Stores the result
// Initialize WordNet
wninit();
// Try to split the input string into words
for (size_t i = 0; i < input.length(); ++i) {
for (size_t j = i + 1; j <= input.length(); ++j) {
std::string substring = input.substr(i, j - i);
// Use WordNet's dictionary lookup functionality to check if the substring is a valid word
if (wn_word(substring.c_str())) {
words.push_back(substring);
i = j - 1; // Update i to skip the found word
break;
}
}
}
// Print the splited words
for (const std::string& word : words) {
std::cout << word << " ";
}
std::cout << std::endl;
// Close the WordNet library
wnclose();
return 0;
}
预期输出:
I LOVE YOU