How to add spaces to a number every 4 digits in C++

1 Answer

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

int main(int argc, char* argv[]) {
    std::string cardNumber = "9003125334656789";

    std::string cardNumberWithSpaces = std::regex_replace(cardNumber, std::regex(".{4}"), "$0 ");

    std::cout << cardNumberWithSpaces << std::endl;
}


 
 
/*
run:
 
9003 1253 3465 6789 
 
*/

 



answered May 30, 2024 by avibootz

Related questions

1 answer 218 views
2 answers 245 views
2 answers 257 views
2 answers 261 views
1 answer 238 views
1 answer 201 views
2 answers 262 views
...