How to replace multiple consecutive question marks (?) with a single question mark in C++

1 Answer

0 votes
#include <iostream>
#include <string>
#include <regex>

int main() {
    std::string input = "Hello??? How are you?? What is your Wi-Fi password????";
    
    std::regex pattern("\\?+"); // Matches one or more consecutive '?'
    std::string result = std::regex_replace(input, pattern, "?");

    std::cout << result << std::endl;
}



/*
run:

Hello? How are you? What is your Wi-Fi password?

*/

 



answered Jul 23, 2025 by avibootz
...