#include <iostream>
#include <regex>
#include <sstream>
#include <string>
/**
* Remove all parentheses and the text inside them.
*
* @param text The input string
* @return The cleaned string
*/
std::string removeParenthesesWithContent(const std::string& text) {
// Remove parentheses and everything inside them
std::regex pattern("\\([^)]*\\)");
std::string cleaned = std::regex_replace(text, pattern, " ");
// Collapse multiple spaces into one (idiomatic C++)
std::istringstream iss(cleaned);
std::ostringstream oss;
std::string word;
bool first = true;
while (iss >> word) {
if (!first) oss << " ";
oss << word;
first = false;
}
return oss.str();
}
int main() {
std::string str =
"(An) API (API) (is a) (connection) connects (between) computer programs";
std::string output = removeParenthesesWithContent(str);
std::cout << output << std::endl;
}
/*
run:
API connects computer programs
*/