Many websites ask for phone numbers. The problem is that there are so many diffe
ID: 3778528 • Letter: M
Question
Many websites ask for phone numbers. The problem is that there are so many different ways to represent a phone number. Examples include 817-555-1234, 817 555 1234 (c), and (817) 555-1234 x23. Write a Raptor program which inputs a string containing a phone number in any format and outputs it in the standard format. For this assignment, the standard format is (817) 555-1234. Your Raptor program should:
1. Input a string including the number
2. Copy only the digits from the input string into another string
3. Issue an error message if the input string does not contain exactly 10 digits
4. Output the phone number in standard format
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
string s;
cout<<"Enter phone number:";
getline(cin,s);//taking user input storing in s
int num[10],i,k=0;
for(i=0;i<s.length();i++)
{
if((int)s[i]>=48&&(int)s[i]<=57)
{ if(k<10)
num[k++]=(int)s[i]-48;//finding numbers
else
k++;
}
}
if(k>10)
{
if(k==12&& s[s.length()-3]=='x')
{}
else {
cout<<"Error :invalid phone number entered"<<" ";//printing error message
return 0;
}
}
cout<<" The Standard Format:";//printing standard format
cout<<"(";
for(i=0;i<10;i++)
{
cout<<num[i];
if(i==2)cout<<") ";
else if(i==5)cout<<"-";
}
cout<<" ";
return 0;
}
ouput:-
Enter phone number:817 555 1234 (c)
The Standard Format:(817) 555-1234
Process exited normally.
Press any key to continue . . .