Question

In: Computer Science

Write a C program that loops and asks a user to enter a an alphanumeric phone...

Write a C program that loops and asks a user to enter a
an alphanumeric phone number and converts it to a numeric one.
No +1 at the beginning. 

You can put all code in one quiz1.c file 
or put all functions except main in phone.c and phone.h
and include it in quiz1.c 
Submit your *.c and .h files or zipped project
*/
#pragma warning (disable: 4996) //windows
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

enum { MaxLine = 255 };
bool isValid(char line[])
{
        //line is a string
        //returns true if line has 10 alnum characters
        //false otherwise
}

void toNumeric(char line[])
{
        //line is a string
        //use the switch statement we did in last lectures
        // (800) GOT - milk ; (800)GOT.milk, etc
        //convert letters to digits; leave rest as is.
}

void puncts_to_spaces(char line[])
{
        //line is a string
        //convert punctuations to spaces: use ispunct
}

void remove_spaces(char line[])
{
        //line is a string
        //remove the spaces and move the digits to the left
        //and put '\0' after the last digit
}

void format_number(char line[])
{
        //assuming the line size is big
        //insert dash or period after 3 and 6 
        size_t size = strlen(line);
        sprintf_s(line, MaxLine, "%c%c%c-%c%c%c-%c%c%c%c", line[0], line[1], line[2], 
                line[3], line[4],line[5], line[6], line[7], line[8], line[9]);
        //you can also do this instead
        //0123456789\0
        //012-345-6789\0
        //\0 is one char '\0'
        /*
        line[12] = '\0';
        line[11] = line[9];
        line[10] = line[8];
        line[9] = line[7];
        line[8] = line[6];
        line[7] = '-';
        line[6] = line[5];
        line[5] = line[4];
        line[4] = line[3];
        line[3] = '-';
        */
}

int main(void)
{
        enum { MaxLen = 255 };
        printf("Welcome to Quiz1\n");

        //read a string from the keyboard and process it
        while (true)
        {
                //code inside the loop
                char s1[MaxLine];
                puts("Enter an alphanumeric phone number (enter to quit) ");
                fgets(s1, MaxLen, stdin); //stdin from keyboard
                        //fgets reads text up to MaxLen-1, EOL including EOL
                s1[strlen(s1) - 1] = '\0'; //remove \n by replacing it with '\0'
                size_t len = strlen(s1);
                if (len == 0)
                        break;
                if (isValid(s1))
                {
                        toNumeric(s1);
                        printf("after toNumeric: ");
                        puts(s1); //fputs(s1, stdout); 
                        puncts_to_spaces(s1);
                        printf("after puncts_to_spaces: ");
                        puts(s1);
                        remove_spaces(s1);
                        printf("after remove_spaces: ");
                        puts(s1);
                        format_number(s1);
                        printf("after format_number: ");
                        puts(s1);
                }
                else
                        printf("invalid alphanumeric phone number\n");
        }
        return (0);
}

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

code.c

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

enum { MaxLine = 255 };
bool isValid(char line[])
{
   int count = 0;
   while(line[count]!='\0'){
       if(!((line[count]>='a' && line[count]<='z')
               || (line[count]>='A' && line[count]<='Z')
               || (line[count]>='0' && line[count]<='9'))){
           return 0;
       }
       count++;
   }
   if(count==10){
       return 1;
   }
   return 0;
}

void toNumeric(char line[])
{
   //line is a string
   //use the switch statement we did in last lectures
   // (800) GOT - milk ; (800)GOT.milk, etc
   //convert letters to digits; leave rest as is.
   int count = 0;
   while(line[count]!='\0'){

       switch (toupper(line[count]))
       {
       case 'A':
       case 'B':
       case 'C':
           line[count]='2';
           break;
       case 'D':
       case 'E':
       case 'F':
           line[count]='3';
           break;
       case 'G':
       case 'H':
       case 'I':
           line[count]='4';
           break;
       case 'J':
       case 'K':
       case 'L':
           line[count]='5';
           break;
       case 'M':
       case 'N':
       case 'O':
           line[count]='6';
           break;
       case 'P':
       case 'Q':
       case 'R':
       case 'S':
       case 'T':
       case 'U':
       case 'V':
           line[count]='8';
           break;
       case 'W':
       case 'X':
       case 'Y':
       case 'Z':
           line[count]='9';
           break;
       }
      
       count++;
   }
}

void puncts_to_spaces(char line[])
{
   //line is a string
   //convert punctuations to spaces: use ispunct
   int count = 0;
   while(line[count]!='\0'){
       if(ispunct(line[count])){
           line[count] = ' ';
       }
       count++;
   }

}

void remove_spaces(char line[])
{
   //line is a string
   //remove the spaces and move the digits to the left
   //and put '\0' after the last digit
   int count = 0;
   while(line[count]!='\0'){
       if(line[count]==' '){

           int count2 = 0;
           while(line[count2]!='\0'){
               line[count2] = line[count2+1];
               count2++;
           }

       }
       count++;
   }
}

void format_number(char line[])
{
   //assuming the line size is big
   //insert dash or period after 3 and 6
   size_t size = strlen(line);
   sprintf(line, "%c%c%c-%c%c%c-%c%c%c%c", line[0], line[1], line[2],
           line[3], line[4],line[5], line[6], line[7], line[8], line[9]);
   //you can also do this instead
   //0123456789\0
   //012-345-6789\0
   //\0 is one char '\0'
   /*
line[12] = '\0';
line[11] = line[9];
line[10] = line[8];
line[9] = line[7];
line[8] = line[6];
line[7] = '-';
line[6] = line[5];
line[5] = line[4];
line[4] = line[3];
line[3] = '-';
   */
}

int main(void)
{
   enum { MaxLen = 255 };
   printf("Welcome to Quiz1\n");

   //read a string from the keyboard and process it
   while (true)
   {
       //code inside the loop
       char s1[MaxLine];
       puts("Enter an alphanumeric phone number (enter to quit) ");
       fgets(s1, MaxLen, stdin); //stdin from keyboard
       //fgets reads text up to MaxLen-1, EOL including EOL
       s1[strlen(s1) - 1] = '\0'; //remove \n by replacing it with '\0'
       size_t len = strlen(s1);
       if (len == 0)
           break;
       if (isValid(s1))
       {
           toNumeric(s1);
           printf("after toNumeric: ");
           puts(s1); //fputs(s1, stdout);
           puncts_to_spaces(s1);
           printf("after puncts_to_spaces: ");
           puts(s1);
           remove_spaces(s1);
           printf("after remove_spaces: ");
           puts(s1);
           format_number(s1);
           printf("after format_number: ");
           puts(s1);
       }
       else
           printf("invalid alphanumeric phone number\n");
   }
   return (0);
}


Related Solutions

Write a C program that loops and asks you every time to enter the name of...
Write a C program that loops and asks you every time to enter the name of a text file name, then it will read the file and perform statistics on its contents and display the results to the screen and to an output file called out.txt. The input files can be any text file. The program should calculate these integers as it reads character by character as we did in the last class example filecopy and make use of the...
Write a C++ program that asks the user to enter the monthly costs for the following...
Write a C++ program that asks the user to enter the monthly costs for the following expenses incurred from operating your automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and a projected total annual cost of these expenses. Label each cost. The labels should be left aligned and have a column width of 30 characters. The cost should be aligned right and displayed with two decimal places...
C++ write a program that asks the user to enter the hours and rate then calculate...
C++ write a program that asks the user to enter the hours and rate then calculate the gross pay for an employee, the program should test if the hours are regular (40), any hour more than 40 should be paid with the overtime rate: 1.5*rate. The program should ask repeatedly the user if he/she wants to continue: y or n, if the user types y, then the program should ask for the hours and rate for another employee then display...
write a program in c++ that asks the user to enter their 5 test scores and...
write a program in c++ that asks the user to enter their 5 test scores and calculates the most appropriate mean. Have the results print to a text file and expected results to print to screen.
In the space provided below write a C++ program that asks the user to enter their...
In the space provided below write a C++ program that asks the user to enter their quarterly earnings for the past two years stores the data in a 2-dimensional array. The program then computes both the annual earnings as well as the total earning and prints the results along with the 2-dimensional array on screen as well as onto a file.
C++. Write a program that asks the user to enter a single word and outputs the...
C++. Write a program that asks the user to enter a single word and outputs the series of ICAO words that would be used to spell it out. The corresponding International Civil Aviation Organization alphabet or ICAO words are the words that pilots use when they need to spell something out over a noisy radio channel. See sample screen output for an example: Enter a word: program Phonetic version is: Papa Romeo Oscar Golf Romeo Alpha Mike The specific requirement...
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
In the space provided below write a ******C program********* that asks the user to enter their...
In the space provided below write a ******C program********* that asks the user to enter their quarterly earnings for the past two years stores the data in a 2-dimensional array. The program then computes both the annual earnings as well as the total earning and prints the results along with the 2-dimensional array on screen. Using the embed icon shown above, also include screenshots demoing the execution of your program. Please write carefully. Thank you
in C++, Write a program that asks the user to enter 6 numbers. Use an array...
in C++, Write a program that asks the user to enter 6 numbers. Use an array to store these numbers. Your program should then count the number of odd numbers, the number of even numbers, the negative, and positive numbers. At the end, your program should display all of these counts. Remember that 0 is neither negative or positive, so if a zero is entered it should not be counted as positive or negative. However, 0 is an even number....
Write a C program that asks the user to enter any two integernumbers, and each...
Write a C program that asks the user to enter any two integer numbers, and each number consists of four-digits. Your program should check whether the numbers are four digits or not and in case they are not a four digit number, the program should print a message and exit, otherwise it should do the following:Print a menu as follows:Select what you want to do with the number 1-3:1- Print Greatest Common Divisor (GCD) of the two numbers.2- Print sum...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT