Hi, I am learning C and am stuck on something.
I have an array:
char phoneNumber [11] = "5558675309";
and I need to print the phone number array in this format: 555-867-5309.
I am lost and not sure how to accomplish that without brute forcing it.
Hi, I am learning C and am stuck on something.
I have an array:
char phoneNumber [11] = "5558675309";
and I need to print the phone number array in this format: 555-867-5309.
I am lost and not sure how to accomplish that without brute forcing it.
Give an example of "brute forcing it." Since this sounds like a display problem, brute forcing may be good enough, honestly. That or it is easy to improve it to be good enough.
I am not sure what you mean with brute force.
A simple way:
Easier in C++Code:char buffer[5] = {0}; strncpy(buffer, phoneNumber, 3); printf("%s-", buffer); strncpy(buffer, phoneNumber+3, 3); printf("%s-", buffer); strncpy(buffer, phoneNumber + 6, 4); printf("%s", buffer);
Code:std::string phoneNumber("5558675309"); phoneNumber.insert(phoneNumber.begin() + 3, '-'); phoneNumber.insert(phoneNumber.begin() + 7, '-'); std::cout << "PhoneNumber: " << phoneNumber << "\n\n";
Why bother, I guess.
Code:printf("%.3s-%.3s-%s\n", phoneNumber, &phoneNumber[3], &phoneNumber[6]);