Create a program that will check the format for telephone numbers. Telephone num
ID: 3632052 • Letter: C
Question
Create a program that will check the format for telephone numbers. Telephone numbers should be entered as a string with the ?rst threecharacters followed by a dash and the last four numbers, e.g., 000-0000.
The program will check that a number entered contains eight characters
and has a dash after the ?rst three characters. Use string functions to
do the validation. Output a message that states whether the telephone
number is or is not in the proper format.
Write a piece of code which prints the characters in a cstring in a
reverse order
char s[10] = "abcde";
char* cptr;
//Your code here.
c++ is the one i need the code in.
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
//PROTOTYPES
bool isProperPhoneFormat(char *phoneString);
//LOCAL DECLARATIONS
const int BUF_SIZE = 100;
char buf[BUF_SIZE];
//PROCEDURES
cout << "Enter the telephone number: ";
cin.getline(buf, BUF_SIZE);
if (isProperPhoneFormat(buf))
{
cout << "The phone number is in the PROPER format. ";
}
else
{
cout << "The phone number is NOT in the proper format. ";
}
cin.get();
return 0;
}
//---------------------------------------------------------
// FUNCTION DEFINITIONS
//---------------------------------------------------------
bool isNumChar(char chr) // check whether chr is numeric character
{
return (chr >= '0' && chr <= '9');
}
//---------------------------------------------------------
bool isProperPhoneFormat(char *phoneString)
{
bool isNumChar(char chr);
const int PROPER_PHONE_LEN = 8;
char *ptr = phoneString;
int phoneStringLength = 0;
//check proper length
while (*ptr++ != 0)
{
phoneStringLength++;
}
if (phoneStringLength > PROPER_PHONE_LEN)
{
return false;
}
//check first 3 numberic characters
ptr = phoneString;
while (ptr - phoneString < 3)
{
if (!isNumChar(*ptr++))
{
return false;
}
}
//check the dash character
if (*ptr++ != '-')
{
return false;
}
//check last 4 numberic characters
while (ptr - phoneString < PROPER_PHONE_LEN)
{
if (!isNumChar(*ptr++))
{
return false;
}
}
return true; //if phoneString satisfies all the conditions above
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.