I'm trying to make a function that filters out all the punctuation and spaces in a sentences and returns only the letters and numbers in a new string.
ex. If I type: Hi, my name is Zach1234. I want it to return only: himynameiszach1234
Yet it it keeps returning only the first letter. Any ideas on how to remedy this problem?
#include <iostream>
#include <cctype>
using namespace std;
string filter(string str)
{
string result = "";
for(int i = 0; i < (str.size()-1); i++)
{
if(isspace(str[i]))continue;
if(ispunct(str[i]))continue;
if(isdigit(str[i]))result += str[i];
if(isalpha(str[i]))result += str[i];
}
return(result);
}
int main()
{
string happy;
cout <<"Please enter a string\n";
cin >> happy;
cout << filter(happy) << endl;
return(0);
}