c++ Many websites ask for phone numbers. The problem is that there are so many d
ID: 3582494 • Letter: C
Question
c++
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.
Sample Output(inputs in bold) Please enter a phone number: 817-555-1234 The properly formatted number is (817) 555-1234 Please enter a phone number: (817)515 7259 x23 The phone number must have 10 digits Please enter a phone number: 214 555 9999 (c) The properly formatted number is (214) 555-9999 Please enter a phone number: 800**4444xxx333 The properly formatted number is (800) 444-4333
Explanation / Answer
Here goes the solution code for the above question
Code:
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <cmath>
#include <ostream>
using namespace std;
int main()
{
cout << "Enter a character string phone number (###.aaa.aaaa):" << endl;
string phoneChar;
getline(cin, phoneChar);
char characters[14];
characters[0]='(';
characters[4]=')';
characters[5]=' ';
characters[9]='-';
int length=phoneChar.size();
int j=1;
for(int i=0; i < length; i++)
{
switch (phoneChar[i])
{
case '1':
characters[j]='1';
j++;
break;
case '2':
characters[j]='2';
j++;
break;
case '3':
characters[j]='3';
j++;
break;
case '4':
characters[j]='4';
j++;
break;
case '5':
characters[j]='5';
j++;
break;
case '6':
characters[j]='6';
j++;
break;
case '7':
characters[j]='7';
j++;
break;
case '8':
characters[j]='8';
j++;
break;
case '9':
characters[j]='9';
j++;
break;
case '0':
characters[j]='0';
j++;
break;
default:
break;
}
while(j==0 ||j==4||j==5||j==9){
j++;
}
if(j==14)
break;
}
cout<<"phone number is: ";
for(int k=0;k<14;k++){
cout<<characters[k];
}
return 0;
}
Example:
817-555-1234 gives you (817) 555-1234
817-555-1234 x26 gives you (817) 555-1234
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.