You guys are mean. You make me do my own research.
Thank you.
Furthering my progress away from String class to C-strings
I want to turn a U.S. phone number into a readable number.
For example, turn "19785551212" into "1-978-555-1212" for human reading.
This code works as expected. Is there a better way to parse a C-string or have I properly figured it out?
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
//--------------------------------
//--- Parse the phone number -----
char areaCode[4]; // 978
char prefix[4]; // 555
char phone[5]; // 1212
char str1[] = "19785551212"; //Starting here...
char str2[40]; //Holds the formatted phone number
char str3[4]; //Area code 978
char str4[4]; //Prefix 555
char str5[5]; //Phone 1212
strncpy ( str3, str1 + 1, 3 ); //Area code, 3 characters
str3[3] = '\0';
strncpy ( str4, str1 + 4, 3 ); //Prefix, 3 characters
str4[3] = '\0';
strncpy ( str5, str1 + 7, 4 ); //Phone, 4 characters
str5[4] = '\0';
sprintf(str2, "1-%s-%s-%s", str3, str4, str5);
Serial.println(str2);
}
void loop() {
}