In: Computer Science
(C++) keep things as simple as possible. I would love a good starting point with hints to be able to finish it!
Write a function called is valid phone number that takes a phone number as an array of characters and its size, and returns true if the input array is a valid phone number, false otherwise. A phone number is valid only if it is of the following format: (uuu) uuu-uuuu where u is from ‘0’, ‘1’, ‘2’, etc. The parentheses, hypen and the space after ’)’ are necessary. Do not use the string or regex libraries
#include <iostream>
using namespace std;
//to check if u is in '0' to '9'
bool ir(char a)
{
if(a>='0' && a<='9')
return true;
else
return false;
}
//checking if given phone number is valid or not
//(uuu) uuu-uuuu (111) 985-1235
bool isvalid(char *str,int size)
{
if(size!=14)
return false;
else
{
bool ans=false;
if ((str[0]=='(') && (str[4]==')') && (str[5]==' ') && (str[9]=='-'))
{
if(ir(str[1]) && ir(str[2]) && ir(str[3]) && ir(str[6]) && ir(str[7]) && ir(str[8]) && ir(str[10]) && ir(str[11]) && ir(str[12]) && ir(str[13]) )
ans=true;
}
return ans;
}
}
int main()
{
int size;
cout << "Enter size: ";
cin >> size;
char str[size], ab[2];
cin.getline( ab,2);
cout << "Enter phone number: ";
cin.getline( str,size+1);
if(isvalid(str,size))
cout<<"phone number is valid.";
else
cout<<"INVALID phone number";
return 0;
}
//SAMPLE OUTPUT
//PLEASE LIKE IT RAISE YOUR THUMBS UP
//IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT
SECTION