Write a function, named “format_phone_number”, that takes in a const reference to a string and returns a new string. The input string is phone number form of ” XXX XXXX ” where there can be whitespace before, after, and in the middle of the two parts of the local phone number. The output phone number should be an internationally valid phone number of the form “1+ (517) XXX-XXX”.
I recommend using StringStreams in writing your function.
use c++
Expert Answer
#include <cstdlib>
#include <fstream>
using namespace std;
string format_phone_number(string &input){
string res =”1+ (517) “;
int count = 0;
for(int i=0;i<input.size();++i){
if(input.at(i)!=’ ‘){
++count;
res = res + input.at(i);
if(count==3)
res = res + “-“;
}
}
return res;
}
int main()
{
string str;
cout<<“Enter a string: “;
getline(cin,str);
cout<<format_phone_number(str)<<endl;
return 0;
}
==============================================
See Output