I am using getline() function to get ride of special characters and punctuation in a sentence, so that when i display the words contained in the sentence, it does not have any other character beside a-z (or A-Z). The problem is that it gets long, and I don't think it is really efficient. I would like to know if I can do it in a efficient way. I am using Dev-C++, the code below is in C++. Thanks for your help.
#include <string>
#include <iostream>
#include <ctype.h>
#include <sstream>
using namespace std;
int main()
{
int i=0;
char y;
string prose, word, word1, word2;
cout << "Enter a sentence: ";
getline(cin, prose);
string mot;
stringstream ss(prose);
y=prose[i++];
if (y=' ') // if character space is encoutered...
cout<<endl << "list of words in the prose " << endl;
cout << "---------------------------"<<endl;
while(getline(ss, word, y)) //remove the space...
{
stringstream ss1(word);
while(getline(ss1, word1, ',')) //remove the comma...
{
stringstream ss2(word1); //remove the period
while(getline(ss2, word2, '.'))
cout<< word2 <<endl; //and display just the word without space, comma or period.
}
}
cout<<'\n';
system ("Pause");
return 0;
}
#############################output
Enter a sentence: What? When i say: "Nicole, bring me my slippers, and give me m y night-cap," is that prose?
What? When i say: "Nicole bring me my slippers and give me my night-cap " is that prose?
Press any key to continue . . .
Use std::remove_if()
:
std::string s(":;[{abcd 8239234");
s.erase(std::remove_if(s.begin(),
s.end(),
[](const char c) { return !isalpha(c); }),
s.end());
If you do not have a C++11 compiler, define a predicate instead of using a lambda (online demo http://ideone.com/NvhKq).