How to mask sensitive parts of a string (e.g. email addresses) in C++

1 Answer

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

std::string maskEmail(const std::string& email) {
    auto atPos = email.find('@');
    if (atPos == std::string::npos || atPos < 2) return email;

    std::string masked(atPos - 1, '*');
    
    return email[0] + masked + email.substr(atPos);
}


int main() {
    std::cout << maskEmail("john.doe@example.com");
}

 
 
/*
run:
 
j*******@example.com

*/

 

 



answered Dec 23, 2025 by avibootz
...