fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define SIZE 100
  6.  
  7. int main(void)
  8. {
  9. char phoneNumber[SIZE];
  10.  
  11. char areaCode[10] = "";
  12. char firstPart[10] = "";
  13. char lastPart[10] = "";
  14.  
  15. char completeNumber[20] = "";
  16.  
  17. printf("Enter a telephone number in the form ");
  18. printf("(555) 555-5555:\n");
  19.  
  20. fgets(phoneNumber, SIZE, stdin);
  21.  
  22. char *tokenPtr = strtok(phoneNumber, " ()-\n");
  23.  
  24. if (tokenPtr != NULL)
  25. {
  26. strcpy(areaCode, tokenPtr);
  27. }
  28.  
  29. tokenPtr = strtok(NULL, " ()-\n");
  30.  
  31. if (tokenPtr != NULL)
  32. {
  33. strcpy(firstPart, tokenPtr);
  34. }
  35.  
  36. tokenPtr = strtok(NULL, " ()-\n");
  37.  
  38. if (tokenPtr != NULL)
  39. {
  40. strcpy(lastPart, tokenPtr);
  41. }
  42.  
  43. strcat(completeNumber, firstPart);
  44. strcat(completeNumber, lastPart);
  45.  
  46. int areaCodeValue = atoi(areaCode);
  47. long phoneNumberValue = atol(completeNumber);
  48.  
  49. printf("\nArea code = %d\n", areaCodeValue);
  50. printf("Phone number = %ld\n", phoneNumberValue);
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0s 5332KB
stdin
Standard input is empty
stdout
Enter a telephone number in the form (555) 555-5555:

Area code = 0
Phone number = 0