Lets say I have something as such:
string s;
cout << "Enter a word:";
cin >> s;
//clean s
print(s);
If someone enters something like --sorry
or what,
, I want to be able to 'clean' the string before passing it into the function which prints out the word. Cleaning a string is making it a word. In this case, a word would be defined as any consecutive strings of letters (also apostrophes can be there, for example don't
is one word. So getting rid of anything else in there.
I was thinking something like this:
string s;
cout << "Enter a word:";
cin >> s;
//clean s
string s2;
for (int i = 0; i < s.length(); i++)
if(isalpha(s[i]) || s[i] = "'")
s2[i] = s[i];
print(s2);
But this makes my program not work, so I need to figure out another way to do this.
Thank you!
edit:
string s;
cout << "Enter a word:";
cin >> s;
//clean s
s.erase(remove_if(s.begin(), s.end(), [](char c) {return !isalpha(c) ||
c == '\'' || c == '\-' || c == ','; }));
print(s);
Edit (latest):
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
string s;
cout << "Enter a word:";
cin >> s;
//clean s
s.erase(remove_if(s.begin(), s.end(), [](char c)
{
return !isalpha(c) || c == '\'' || c == '-' || c == ',' || c == ';' || c == ':';
}));
cout << s;
}