Part 1: 1) Write a C program getPhoneNumber.c that accepts a phone number from the user in the form XXX-XXX-XXXX, and then displays it in the form (XXX)XXX-XXXX: Enter a date (XXX-XXX-XXXX): 011-123-4567 You entered (011)123-4567

Respuesta :

Answer:

Following are the program, which can be below:

#include <stdio.h> //defining header file

#include <string.h>//defining header file

int main() //defining main method

{  

int x=0,y=0,l=0; //defining integer variable

char phone_number[32] = {'\0'}; //defining char array

printf("Enter a date(xxx-xxx-xxxx): \n "); //print message

printf("Example: 000-000-0000 \n :"); //print message

scanf("%s",phone_number); // taking input from user

printf("%s\n",phone_number); //print number

for(x=0; phone_number[x]; x++) // check character by character

{

if(x==0) // put ( on first character

{

l = strlen(phone_number); //calculate length  

phone_number[l] = '\0'; //check number  

for(y=l; y>=1; y--) //loop to calculate a value

{

phone_number[y] = phone_number[y-1]; //holding value in array

}

phone_number[y] = '('; //assigning bracket

}

if(phone_number[x] == '-') // put ) at first -

{

phone_number[x] = ')'; //assigning bracket

break;

}

}

printf("You entered %s\n",phone_number); //print all value with brackets

}

Output:

Enter a date(xxx-xxx-xxxx):  

Example: 000-000-0000  

:011-123-4567

011-123-4567

You entered (011)123-4567

Explanation:

The description of the above program can be defined as follows:

  • In the above program three integer variable "x, y, and l" and a character array "phone_number" is declared.
  • In the char, variable user input the value and print its value by using the print function.
  • In the next line, a for loop is declare, that checks user input value character by character and counts its length by using the strlen() method, inside the loop a conditional statement is used.
  • In the condition statement check, the values are not equal to null and provide brackets in the inserted value.