fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(void) {
  6. char phone[20];
  7. printf("Enter phone number in format (555) 555-5555: ");
  8. fgets(phone, sizeof(phone), stdin);
  9.  
  10. char *areaCodeStr = strtok(phone, "() -");
  11. char *first3 = strtok(NULL, "() -");
  12. char *last4 = strtok(NULL, "() -");
  13.  
  14. char fullNumberStr[20] = "";
  15. if (first3 && last4) {
  16. strcat(fullNumberStr, first3);
  17. strcat(fullNumberStr, last4);
  18. }
  19.  
  20. int areaCode = atoi(areaCodeStr);
  21. long phoneNumber = atol(fullNumberStr);
  22.  
  23. printf("Area code (int): %d\n", areaCode);
  24. printf("Phone number (long): %ld\n", phoneNumber);
  25.  
  26. return 0;
  27. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Enter phone number in format (555) 555-5555: Area code (int): 0
Phone number (long): 0