In this lab, you will write a program that will read a canonical phone number form the user, then prints it back to the properly formatted in the for “xxx-xxxx”. You program must achieve the following requirements: 1. If the user enters the number “0”, the program should exit immediately. 2. If the user enters any phone number that is grater than or less than 7-digits, the program should report an error back. In other words, only valid 7-digit numbers are accepted, so entering an 8-digits number or 5-digits number for example should trigger an error message. 3. If the users enters an invalid phone number, then the program should report the error to the user. Hint: central office code doesn’t start with “0” or “1”. 4. If the user enters a valid 7-digit numbers, then the program should format the phone number in a hyphenated 7-digit representation, i.e, “7274723” will become “727-4723”. 5. Bonus: You get 2 bonus marks if the program continue looping infinitely until the user explicitly enters “0”.
i need a c langeuage code
Thanks for the question.
Below is the code you will be needing Let me know if you have
any doubts or if you need anything to change.
Thank You !!
===========================================================================
#include<stdio.h>
#include<string.h>
int digitCount(char phone_number[]){
int i=0;
int digits=0;
while(phone_number[i]!='\0'){
char letter =
phone_number[i];
if('0'<=letter &&
letter<='9')digits++;
i++;
}
return digits;
}
void printFormatttedPhoneNumber(char phone_number[]){
int i=0;
int digits=0;
printf("Formatted phone number: ");
while(phone_number[i]!='\0'){
char letter =
phone_number[i];
if('0'<=letter &&
letter<='9'){
digits++;
if(digits==4)printf("-%c",letter);
else
printf("%c",letter);
}
i++;
}
printf("\n");
}
int main(){
char phone_number[20];
int digits=0;
while(1){
printf("Enter the phone number:
");
scanf("%19s",phone_number);
if(strcmp(phone_number,"0")==0)break;
digits=digitCount(phone_number);
if(digits==7){
printFormatttedPhoneNumber(phone_number);
}else{
printf("ERROR.The phone number is not correct.\n");
}
}
}
=============================================================
Get Answers For Free
Most questions answered within 1 hours.