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 108 views
2 answers 116 views
2 answers 141 views
2 answers 128 views
1 answer 139 views
1 answer 115 views
2 answers 128 views
...