replacing a letter with another.

I want to replace every letter “a” in a string with the letter “e”, and return the count of the number of “a”s that we changed. My main function is supposed to be as follows but I dont know what my callimg functions signatures should be. I suppose it should be somewhat like thia bit still I dont know how to implement this function.I am a total noob. Can anybody help me??

int replaceAWithE(______string____ s) {


}
//
1
2
3
4
5
6
7
8

int main()
{
string s1 = "aardvark";
int numAChanged = replaceAWithE(s1); 
cout << s1 << " num a changed: " << numAChanged << endl;}
// outputs: eerdverk num a changed: 3
Last edited on
It sounds like you want to pass your string by reference so that modifying the reference in the function modifies the actual object.

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

int replaceAWithE(std::string& s)
{
    int num_changed = 0;
    
    // ...
    // for example, replace index 0 (first index) with z
    s[0] = 'z';
    
    return num_changed;
}

int main()
{
    std::string s1 = "aardvark";
    int numAChanged = replaceAWithE(s1);
    
    // outputs: eerdverk num
    std::cout << s1 << " num a changed: " << numAChanged << std::endl;
}
Last edited on
1
2
3
4
int replaceAWithE(______string____ s) {


}


In addition, some identifiers are reserved for use by C++ implementations and shall not be used otherwise; no diagnostic is required.

- Each identifier that contains a double underscore __ or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use.
http://eel.is/c++draft/lex.name
A string is essentially an array of characters. You just look at each and replace if necessary.

One could use standard function std::replace for it:
http://www.cplusplus.com/reference/algorithm/replace/

Alas, that does not count. For that there is std::count:
http://www.cplusplus.com/reference/algorithm/count/


If you do manage to read those two pages, then you might notice that both are simple loops and you can combine them (for practice and efficiency). See
http://www.cplusplus.com/doc/tutorial/control/
http://www.cplusplus.com/reference/string/string/operator[]/


Edit:
Later, when you already master the basics, you could perhaps tell what happens here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <algorithm>    // std::replace_if
#include <string>

int main () {
  std::string s2 ("test string");

  size_t num {};
  std::replace_if( s2.begin(), s2.end(),
    [&num](char c){ if ('t' == c) { ++num; return true; } return false; },
    'X' );

  std::cout << s2 << " num a changed: " << num << '\n';
  return 0;
}
Last edited on
I would create a more general solution like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
int replace(string& txt, char old_char = 'a', char new_char = 'e')
{
  int count = 0;
  for (char& ch: txt)
  {
    if (your logic here)
    {
      // replace old with new char
      cout++;
    }
  }
  return count;
}
Test suite based on Thomas1965's setup. Took out default value

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
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

int Replace(string& s, char old_char, char new_char)
{
    int count = 0;
    for (char& c : s)
    {
        //TODO: fix condition and add logic to modify c
        if (false) 
        {
            count++;
        }
    }
    return count;
}

int main()
{
    vector<string> examples = 
    {
        "mare",
        "tall man",
        "invoking",
        "he wrote, \"yaaaaaaaaaaaa!\""
    };
    
    int total;
    for (auto& e : examples)
    {
        cout << setw(30) << right  << e << " --> ";
        total = Replace(e, 'a', 'e');
        cout << setw(30) << left  << e << 
                setw(5) << total << endl;
    }
    
    return 0;
}


Current output, still missing the logic:
                          mare --> mare                          0    
                      tall man --> tall man                      0    
                      invoking --> invoking                      0    
    he wrote, "yaaaaaaaaaaaa!" --> he wrote, "yaaaaaaaaaaaa!"    0    
Last edited on
Topic archived. No new replies allowed.