Skip to content

Commit

Permalink
Missing from Guide
Browse files Browse the repository at this point in the history
These two are missing from the guide
  • Loading branch information
psigillito committed Mar 29, 2016
1 parent dfa9188 commit 3e658d8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
32 changes: 32 additions & 0 deletions ch17/17_25.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//exercise 17.25
//Rewrite your phone program so that it writes only the first phone number for each person

#include <iostream>
#include <regex>
#include <string>

using namespace std;

string pattern = "(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})";
string fmt = "$2.$5.$7";
regex r(pattern);
string s;

int main()
{
while(getline(cin,s))
{
smatch result;
regex_search(s,result,r);
if(!result.empty())
{
cout<<result.prefix()<<result.format(fmt)<<endl;
}
else
{
cout<<"Sorry, No match."<<endl;
}
}

return 0;
}
35 changes: 35 additions & 0 deletions ch17/17_27.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//exercise 17.27
//Write a program that reformats a nine-digit zip code as ddddd-dddd.

#include <iostream>
#include <regex>
#include <string>

using namespace std;

string pattern = "(\\d{5})([.- ])?(\\d{4})";
string fmt = "$1-$3";

regex r(pattern);
string s;


int main()
{
while(getline(cin,s))
{
smatch result;
regex_search(s,result, r);

if(!result.empty())
{
cout<<result.format(fmt)<<endl;
}
else
{
cout<<"Sorry, No match."<<endl;
}

}
return 0;
}

0 comments on commit 3e658d8

Please sign in to comment.