In c++ Write a function that takes a 12-character string specifying the phone nu
ID: 3875557 • Letter: I
Question
In c++ Write a function that takes a 12-character string specifying the phone number in E.164 format and returns the local phone number as a string. Here’s a description of what this function does:
Country code = 1. The first two digits will be +1, followed by a three-digit area code, and the remaining 7 digits specify the subscriber number. Your function should format the number using parentheses around the area code, and dashes between the area code and the two parts of rest of the number. o EXAMPLE: The E.164 format for the main number is +12012812000. Your function should return “(201)-281-2000”. Country code = 33. The first three digits will be +33, followed by nine digits. When you format the number, you will prepend a 0 digit to the start. You should insert a space character between each pair of digits. o EXAMPLE: The E.164 format for the Louvre Museum phone number is +133140205990. Your function should return “01 40 20 59 90”. Country code = 852. The first four digits will be +852, followed by eight digits. When you format the number, you will insert a space character between the two 4-digit pairs of numbers. o EXAMPLE: The E.164 format for the Star Ferry phone number is +85223677065. Your function should return “2367 7065”.
Explanation / Answer
In c++ Write a function that takes a 12-character string specifying the phone number in E.164 format and returns the local phone number as a string.
Program:-
//Header Files
#include <iostream>
#include<string>
using namespace std;
//Function declaration
void getString(string,string);
//Main method
int main() {
string cc,phNo;
cout<<"Enter country code"<<endl; //Enter country code
cin>>cc;
cout<<"Enter Phone number"; //Enter phone number
cin>>phNo;
getString(phNo,cc); //Function call
return 0;
}
//Function defenition
void getString(string str,string cc){ //Function with two parameters
if(cc.compare("1")==0){ //Compare country code=1
string str1,str2,str3;
//Convert phone number to paranthesis and dash format
for(int i=2;i<str.size();i++){
if(i<5){
str1+=str[i];
}
else if(i<8){
str2+=str[i];
}
else{
str3+=str[i];
}
}
cout<<"("<<str1<<")-"<<str2<<"-"<<str3; //Display
}
//Compare country code 33 and Convert phone number to space format
else if(cc.compare("33")==0){
string str1="0",str2,str3,str4,str5;
for(int i=4;i<str.size();i++){
if(i<5){
str1+=str[i];
}
else if(i<7){
str2+=str[i];
}
else if(i<9){
str3+=str[i];
}
else if(i<11){
str4+=str[i];
}
else {
str5+=str[i];
}
}
cout<<str1<<" "<<str2<<" "<<str3<<" "<<str4<<" "<<str5; //Display output
}
//Compare country code 852 and Convert phone number to specified format
else {
string str1;
for(int i=4;i<str.size();i++){
str1+=str[i];
}
cout<<str1; //Display output
}
}
-------------------------------------------------------------------------------------------------------------------------------
Output
--------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.