1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
//function prototypes
string extractField( string &input, char delimiter = '#' ); // added a second parameter with a default value
string fixName( string &input );
string fixSSN( string &input );
string fixPhone( string &input );
string fixAddress( string &input );
string fixCity( string &input );
string fixState( string &input );
string getZip( string &input );
void buildLine( string name, string SSN, string phone, string addr, string city, string state, string zip, ostream &fout );
int main()
{
string input, name, SSN, phone, addr, city, state, zip;
input = "john smith#16598007@8148337965#3rd & state st#erie#pa#16506" ;
std::cout << input << '\n' ;
{
name = fixName( input );
SSN = fixSSN( input );
phone = fixPhone( input );
addr = fixAddress( input );
city = fixCity( input );
state = fixState( input );
zip = getZip( input );
buildLine( name, SSN, phone, addr, city, state, zip, cout );
input.clear();
}
// cout << "Complete" << '\n';
// return 0;
}
string extractField( string &input, char delimiter )
{
// int num;
// string s;
// num = input.find( '#', 0 );
const auto num = input.find( delimiter, 0 );
// num -= 1;
// s = input.substr( num, 0 );
string s = input.substr( 0, num ) ;
// s = input.substr(num, 0);
input.erase( 0, num + 1 );
return s;
}
string fixName( string &input )
{
// char temp;
// int num;
// string first, last, name;
string name = extractField( input );
auto num = name.find( ' ', 0 );
// num -= 1;
string first = name.substr( 0, num );
// name.erase( 0, num );
name.erase( 0, num + 1 ) ;
string last = name;
// cout << first << last << '\n';
/*
temp = first.front();
temp = toupper( temp );
first.erase( 0, 1 );
first.insert( 0, 1, temp );
*/
first.front() = toupper( first.front() ) ;
/*
temp = last.front();
temp = toupper( temp );
first.erase( 0, 1 );
last.insert( 0, 1, temp );
*/
last.front() = toupper( last.front() ) ;
// last.append( 1, ',' );
// first.append( 1, ',' );
return last + ", " + first + ", ";
}
string fixSSN( string &input )
{
// string SSN;
// SSN = extractField( input );
string SSN = extractField( input, '@' );
SSN.insert( 4, 1, '-' );
SSN.insert( 7, 1, '-' );
// SSN.append( 1, ',' );
SSN += ", " ;
return SSN;
}
string fixPhone( string &input )
{
// string phone;
string phone = extractField( input );
phone.insert( 4, 1, '-' );
phone.insert( 8, 1, '-' );
// phone.append( 1, ',' );
return phone + ", " ;
}
string fixAddress( string &input )
{
// string addr;
const string addr = extractField( input );
// addr.append( 1, ',' );
return addr + ", ";
}
string fixCity( string &input )
{
// string city;
// int temp;
string city = extractField( input );
/*
temp = city.front();
temp = toupper( temp );
city.erase( 0, 1 );
city.insert( 0, 1, temp );
*/
city.front() = toupper( city.front() ) ;
// city.append( 1, ',' );
return city + ", " ;
}
string fixState( string &input )
{
// string state;
// int temp;
string state = extractField( input );
/*
temp = state.front();
temp = toupper( temp );
state.erase( 0, 1 );
state.insert( 0, 1, temp );
temp = state.back();
temp = toupper( temp );
state.erase( 1, 1 );
state.insert( 1, 1, temp );
*/
for( char& c : state ) c = toupper( c ) ;
return state + ' ' ;
}
string getZip( string &input )
{
string zip;
zip = input;
return zip;
}
void buildLine( string name, string SSN, string phone, string addr, string city, string state, string zip, ostream &fout )
{
fout << name << SSN << phone << addr << city << state << zip << '\n';
}
|