String filter function. HELP?!

I'm new to c++ and I got this new assignment. I missed a day in class and now im confused. Here's the question:

Please write a function called Filter that takes a string and returns a new string that has just the letters (A-Z and a-z) from that string.

The function should not output anything, only return a string.

Also, write the driver (main) function to test it and make sure that it has input/ouput like below.

I got this sample code from a friend on the function and I'm pretty sure it's wrong:

1
2
3
4
5
6
7
8
9
string filter (string s){
	string r = " ";
		for (int i = 0; i<s.length(); i++){
		char c=s[i];
		if (is alpha(c));
		r = toupper(c);
	}
	return r;
}


any kind of help would be appreciated
returns a new string that has just the letters (A-Z and a-z) from that string.


Dont understand what you mean. Could you elaborate and give an example?
The program should run like this:

1
2
Enter a string: Do geese see God?
Filtered string is: "DogeeseseeGod"

if that helps
Line 6 should use +=, not =. Also not sure why you're using toUpper.
The program should run like this:

1
2
Enter a string: Do geese see God?
Filtered string is: "DogeeseseeGod 


Wouldnt it have been easier to just say the function is supposed to remove the spaces...

Are you allowed to use functions? Because then you can just use the remove/erase function

str.erase(remove(str.begin(), str.end(), ' '), str.end());

Also. When you send in a string like that, it sends in a copy, so its already a different string. So do you have to create another one?

1
2
3
4
5
string filter (string s){
              s.erase(remove(s.begin(), s.end(), ' '), s.end());
              return s;
} /* if you have to create a new string. Just make the new string equal "s" and then do       
  switch out all the "s" for the new string.*/
Last edited on
ok, fg109. Is there anything wrong with the function?
Ok, but how do you call that with a main function... If that makes any sense.
Something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string filter(string s){ // recieve a copy of string begin.
	s.erase(remove(s.begin(), s.end(), ' '), s.end()); // remove all the spaces.
	return s; // return the copy and place it in string begin
}

int main(){

	string begin; // create string
	cout << "Enter a string: "; // tell user to input string
	getline(cin, begin); // input string 

	begin = filter(begin); // call the function and give it a copy of string begin. And whatever
                                        // it returns, put it in put it in string begin.
	
	cout << begin << endl; // output begin
	system("pause");
}
Last edited on
Okay, thank you for the explanation and the help. I think I get it now.
but it says the remove function does not take 3 arguments?
Show me your code.
I'm just trying to get the code you sent to compile. Line 2 of your code says there is an error:
error C2660: 'remove' : function does not take 3 arguments
After that I can figure out the rest.
Works fine for me... hmm Do you have everything included and all that stuff?
i included <iostream> and <string> is there anything else? I'm still getting an error, I even copied and pasted to make sure.
Try including #include <algorithm>
Okay, now it works. Is there any way to get characters like ? or ! filtered out? If you get what im asking...?
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

#include <iostream>//for std::cout and std::cin
#include <string>//for std::string
#include <cctype>//for isalpha function

using namespace std;

string filter(string& s)
{
    //it's essential to continue calculating the string size, given we'll likely be removing elements
    for(int i = 0; i < s.size(); i++)
    {
         if(!isalpha(s[i])) s.erase(i, 1);//if element is not part of the alphabet, remove it 
    }
    return s;
}

int main(void)
{
    string line;
    getline(cin, line);//read a whole line of input
    string n = filter(line);
    cout << endl << n;
    return 0;
}
Last edited on
okay, but what happened to the input in the main function? I just got a little confused, because i haven't seen some of this stuff yet.
The getline() function reads a whole line of input, not just non-space characters as the >> operator would. This allows the line string to contain spaces as well. Since the filter function returns a string, we store the returned value in string n. We can then output n to the screen. The line string was passed by reference, so it was changed by the filter function. So line and n would contain the same information.
Topic archived. No new replies allowed.