How to remove quotes and commas from a string in C++

3 Answers

0 votes
// Manual Filtering 

#include <iostream>
#include <string>

// ------------------------------------------------------------
// removeQuotesAndCommas
// Removes single quotes ('), double quotes ("), and commas (,)
// by manually copying only allowed characters.
// ------------------------------------------------------------
std::string removeQuotesAndCommas(const std::string& input) {
    std::string result;
    result.reserve(input.size()); // small optimization

    for (char c : input) {
        if (c != '\'' && c != '"' && c != ',') {
            result += c;  // keep only allowed characters
        }
    }
    return result;
}

int main() {
    std::string s = "\"Imagine there's, no heaven\", 'Imagine' \"all\", the people.";

    std::string cleaned = removeQuotesAndCommas(s);

    std::cout << "Original: " << s << "\n";
    std::cout << "Cleaned:  " << cleaned << "\n";
}


/*
run:

Original: "Imagine there's, no heaven", 'Imagine' "all", the people.
Cleaned:  Imagine theres no heaven Imagine all the people.

*/

 



answered May 10 by avibootz
0 votes
// erase + remove_if

#include <iostream>
#include <string>
#include <algorithm>

// ------------------------------------------------------------
// removeQuotesAndCommas
// Removes single quotes ('), double quotes ("), and commas (,)
// using erase/remove_if — efficient and idiomatic modern C++.
// ------------------------------------------------------------
std::string removeQuotesAndCommas(std::string input) {

    input.erase(
        std::remove_if(
            input.begin(),
            input.end(),
            [](char c) {
                return c == '\'' || c == '"' || c == ',';
            }
        ),
        input.end()
    );

    return input;
}

int main() {
    std::string s = "\"Imagine there's, no heaven\", 'Imagine' \"all\", the people.";

    std::string cleaned = removeQuotesAndCommas(s);

    std::cout << "Original: " << s << "\n";
    std::cout << "Cleaned:  " << cleaned << "\n";
}



/*
run:

Original: "Imagine there's, no heaven", 'Imagine' "all", the people.
Cleaned:  Imagine theres no heaven Imagine all the people.

*/

 



answered May 10 by avibootz
0 votes
// Using std::regex

#include <iostream>
#include <string>
#include <regex>

// ------------------------------------------------------------
// removeQuotesAndCommas
// Removes single quotes ('), double quotes ("), and commas (,)
// using a regular expression.
// Note: regex is slower, use for simple tasks.
// ------------------------------------------------------------
std::string removeQuotesAndCommas(const std::string& input) {

    // Replace any ', ", or , with an empty string
    return std::regex_replace(input, std::regex("['\",]"), "");
}

int main() {
    std::string s = "\"Imagine there's, no heaven\", 'Imagine' \"all\", the people.";

    std::string cleaned = removeQuotesAndCommas(s);

    std::cout << "Original: " << s << "\n";
    std::cout << "Cleaned:  " << cleaned << "\n";
}



/*
run:

Original: "Imagine there's, no heaven", 'Imagine' "all", the people.
Cleaned:  Imagine theres no heaven Imagine all the people.

*/

 



answered May 10 by avibootz

Related questions

1 answer 103 views
1 answer 332 views
332 views asked Feb 14, 2022 by avibootz
2 answers 228 views
1 answer 133 views
1 answer 106 views
...