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
|
#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>
using namespace std;
void clean_entry( const string& s1, string& s2 ) {
string::const_iterator start = s1.begin();
string::const_iterator finish = s1.begin();
//finds position to start
start = find_if (s1.begin(), s1.end(),
[] (char c) -> bool { return (isalnum(c) > 0); });
//finds position to end
finish = find_if(s1.begin(), s1.end(),
[] (char c) -> bool { return (!isalnum(c) > 0); });
// -------------------------------------------------------------------
// find_if returns a iterator, not a position and substr wants a
// position not an iterator so convert the interator to a position.
// When you were using *start and *finish you were using the values
// that were present at those locations (like 't' or '2') in the
// string which was causing you crash.
// --------------------------------------------------------------------
s2 = s1.substr (start-s1.begin(), finish-start); // purges the string of everything after punctuation
// ---------------------------------------------------------------------
// Have to use transform because for_each is not going to change the
// value of the elements of the string
// for_each (s2.begin(), s2.end(), ::tolower); //converts all letters to lowercase
transform (s2.begin(), s2.end(), s2.begin(),::tolower); //converts all letters to lowercase
}
int main(void){
string s1("444ThISi5ATest99$%.toSeeif This works!!"),s2;
cout << "Before: " << s1 << endl;
clean_entry(s1,s2);
cout << "After: " << s2 << endl;
return 0;
}
|
$ ./a.out
Before: 444ThISi5ATest99$%.toSeeif This works!!
After: 444thisi5atest99 |