Write a Value Returning Filter Type Function

I guess my main problem is that I do not know how to begin assembling this program with the counter controlled loop.


"Write a value returning filter type function called strikeChar that takes a sentence and a char to strike (X) out, and then returns back the string with the given char changed to an 'X'. Use a counter controlled loop and the filter pattern."

Example: cout << strikChar("Hello World" , 'l'); // would print: HeXXo WorXd
Start somewhere (perhaps writing a main function and a frame for the strikChar function) and show us a more specific place where you get stuck.

Some questions you need to answer for yourself:
- What is the return type of strikChar?
- What are the input parameter types of strikChar?

Can you write a for loop that iterates over the characters of a string? That would be a vital step.

http://www.cplusplus.com/doc/tutorial/control/
http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
@Ganado This is as far as I can get until I get stuck.

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

#include <string>
#include <iostream>

using namespace std;

string strikeChar(string s, char c)
{


    for (int i = 1; i < s.length(); i++) {

        if (c == 'l')
        {
            cout << 'X';
        }

        return s;
    }
}

int main()
{


    cout << strikeChar("Hello World", 'l');


}
1) In C++, indices into containers start at 0, not 1. Your loop is iterating one too few times.

2) You're comparing the value of c with the character 'l'. Why? Surely, what you should be doing is comparing the value of c with each cnaracter of the string, to see if that character is the one you need to strike out, yes?

3) The problem description says you're supposed to change the contents of the string if the character matches the specified strikeout character (the value of c). Nowhere are you modifying the string.

4) You're returning from your function at the end of the first iteration of the loop. This means that your loop will never do more than one iteration.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

std::string strikeChar(std::string s, const char c)
{
   for (int i = 0; i < s.length(); i++)
   {
      if (s[i] == c)
      {
         s[i] = 'X';
      }
   }

   return s;
}

int main()
{
   std::string str { "Hello World" };

   str = strikeChar(str, 'l');

   std::cout << str << '\n';
}

HeXXo WorXd

A more efficient way to pass complex data types is by pointer or reference, no need to copy the data as your assignment requires (passing by reference below):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

void strikeChar(std::string& s, const char c)
{
   for (int i = 0; i < s.length(); i++)
   {
      if (s[i] == c)
      {
         s[i] = 'X';
      }
   }
}

int main()
{
   std::string str { "Hello World" };

   strikeChar(str, 'l');

   std::cout << str << '\n';
}
That makes sense. Thank you for your help.
now that you did it with your own loop, take a look at std::replace_if() tool in <algorithm>.
If it is more advanced than you care to deal with right now, it is enough that you know it exists so you can revisit the idea later.
Last edited on
@jonnin, I had "forgotten" about std::replace_if. Combine it with a lambda and get something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <algorithm>

void strikeChar(std::string& s, const char c)
{
   auto IsEquals = [c] (char& str)->bool { return str == c; };

   std::replace_if(s.begin(), s.end(), IsEquals, 'X');
}

int main()
{
   std::string str { "Hello World" };

   strikeChar(str, 'l');

   std::cout << str << '\n';
}
Topic archived. No new replies allowed.