Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to remove parentheses and the text inside them from a string in C++

2 Answers

0 votes
#include <iostream>
#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) {
    std::string result;
    int depth = 0;  // Tracks whether we are inside parentheses

    // Loop through each character
    for (char c : text) {
        if (c == '(') {
            depth++;        // Enter parentheses
            continue;
        }
        if (c == ')') {
            if (depth > 0) depth--;  // Exit parentheses
            continue;
        }
        if (depth == 0) {
            result += c;   // Only copy characters outside parentheses
        }
    }

    // Trim extra spaces created after removal
    std::string cleaned;
    bool space = false;
    for (char c : result) {
        if (std::isspace(static_cast<unsigned char>(c))) {
            if (!space) cleaned += ' ';
            space = true;
        } else {
            cleaned += c;
            space = false;
        }
    }

    // Final trim of leading/trailing spaces
    if (!cleaned.empty() && cleaned.front() == ' ')
        cleaned.erase(cleaned.begin());
    if (!cleaned.empty() && cleaned.back() == ' ')
        cleaned.pop_back();

    return cleaned;
}

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

*/

 



answered May 31 by avibootz
edited May 31 by avibootz
0 votes
#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

*/

 



answered Jun 1 by avibootz

Related questions

...