Answered! Write a function, named "format_phone_number", that takes in a const reference to a string and returns a new…

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.

Question 6: Stringstream Write a function, named format phone-number .that takes in a const reference to a string and ret ns a new string. The input string is phone number form XOX XXOOK where there can be white space before, after, and in the middle of the two parts of the local phone number. The output phone number should be an intemationally valid phone number of the fom recommend using Stringstreams in writing your function. include <string std string 4 string format phone number (const string input Test Cases Save & Run Tests Status 00h Only Middle Space Unit Testcase 002 More white space Unit Testcase Detailsuse c++

Expert Answer

 #include <iostream>

#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

Still stressed from student homework?
Get quality assistance from academic writers!