Question

In this lab, you will write a program that will read a canonical phone number form...

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

Homework Answers

Answer #1

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");
       }
   }
  
}

=============================================================

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
WRITE A JAVA PROGRAM: The last digit of a credit card number is the check digit,...
WRITE A JAVA PROGRAM: The last digit of a credit card number is the check digit, which protects against transaction errors. The following method is used to veryfy credit card numbers. For the simplicity we can assume that the credit card has 8 digits instead of 16. Following steps explains the algorithm in determining if a credit card number is a valid card.  Starting from the right most digit, form the sum of every other digit. For example, if...
Write a complete C++ program asking the user to enter numbers one by one. User enters...
Write a complete C++ program asking the user to enter numbers one by one. User enters only whole numbers 0 and 10. Once the user enters 0, then the program should print sum of only odd numbers entered by user prior to 0. A valid scenario given below: Enter a number: 5 Enter a number: 3 Enter a number: 2 Enter a number: 3 Enter a number: 8 Enter a number: 0 Sum of the odd numbers you entered is...
1)Write a program that asks a user for a number. If (and only if) the number...
1)Write a program that asks a user for a number. If (and only if) the number is greater than zero print “The number is valid” 2)Write a program that asks a user for a grade. If (and only if) the grade is greater than zero and less than 101, print “The grade is valid” 3)Write a program that asks a user how many widgets they want to buy and prints out what the user should pay. Widgets costs 10 dollars....
Please write C++ program. Fibonacci numbers have the property that each consecutive number is a sum...
Please write C++ program. Fibonacci numbers have the property that each consecutive number is a sum of two preceding: 0, 1, 1, 2, 3, 5, 8, 13, 21 ..... Your program should ask user for the integer, and output the result, which shows Fibonacci number at that position. For example if user enters '5', program should output '3' (fifth position in the series). Your program should use recursive function to compute and return back to the main the Fibonacci number....
Days of the week Write a program that asks the user for a number in the...
Days of the week Write a program that asks the user for a number in the range of 1 through 7. The program should display the corresponding day of the week, where 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday, and 7 = Sunday. The program should display an error message if the user enters a number that is outside of the range of 1 though 7.
Write a C-program to print a table (shown below) showing how many times a digit appears...
Write a C-program to print a table (shown below) showing how many times a digit appears in a number. The input number will be entered by the user. For example, if the user enters 378677189, then the output should be as follows: Digit: 0 1 2 3 4 5 6 7 8 9 Frequency: 0 1 0 1 0 0 1 3 2 1 Remember to allow the users to enter their own choice of input number. [Hint: arrays could...
Binary Search Tree Code in Java Write a program that prompts user to enter however many...
Binary Search Tree Code in Java Write a program that prompts user to enter however many numbers they want to add to the binary search tree. Then have the user input numbers until the tree contains that many elements. If the user enters a number that already exists in the tree, then a message should be displayed "Number is already in the tree". Once all elements are added to the tree print its contents in sorted order. Assume all user...
IN C++ PLEASE. Write a program where the user enters a number and you output an...
IN C++ PLEASE. Write a program where the user enters a number and you output an unfilled square of stars. (DO NOT PROMPT THE USER, just take in a number, The only console output should be the squares). For example the program would start with console input, The user would enters 3, you would output (note 3 is the lowest number your program will be tested with) *** * * *** or, The user enters 5, you would output *****...
USE ONLY C LANGUAGE The program should read inputs that look like this: 10 11 56...
USE ONLY C LANGUAGE The program should read inputs that look like this: 10 11 56 92 10 5 42 7 1 33 99 The program should start by reading one integer, which indicates how many numbers there are in the random sequence. The program should then read a sequence of random integers. If fewer numbers are provided than specified (by the first integer on the first line), the program should print the following error message (replace XXX with the...
This is a Java program Program Description You work for a local cell phone company and...
This is a Java program Program Description You work for a local cell phone company and have been asked to write a program to calculate the price of a cell phone data plan being purchased by a customer. The program should do the following tasks: Display a menu of the data plans that are available to be purchased. Read in the user’s selection from the menu.  Input Validation: If the user enters an invalid option, the program should display an error...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT