replacing char in a string.

Hey guys, so in a bit of a bind, and my project is due in a few hours. I know there is a way, but all of our school tutors are not online. So, the issue is, I need to replace a user input letter, in a user input string with a dash. I have the two methods for collecting the letter and string. The trick is, we cannot use the std::replace function in our program, so I am lost as to how I supposed to accomplish this. Currently, my method only adds a dash at the beginning, and doesn't replace all of the key letters in the phrase input by the user.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  #include <iostream>
#include <string>
#include <cmath>


using namespace std;

// method prototypes
char getKeyLetter();
string getstring();
string maskLetter(string theString, char keyLetter);
string removeLetter();
int countKey();

int main()
{
   char keyLetter;
   string theString;

   theString = getstring();
   keyLetter = getKeyLetter();
   getKeyLetter();
   getstring();
   maskLetter(theString, keyLetter);


   cout << "String with '" << keyLetter << "' masked:\n";
   cout << maskLetter(theString, keyLetter) << endl;

   cout << "# of: " << endl;

   return 0;
}

// gets inputs
char getKeyLetter()
{
   string letter = "PLACEHOLDER";
   char keyLetter;
   while (letter.length() != 1)
   {
      cout << "Please enter a SINGLE letter to act as key:";
      cin >> letter;
   }

   keyLetter = letter[0];
   return keyLetter;
}

string getstring()
{
   string theString;
   do {
      cout << "Please enter a phrase or sentence >= 4: \n";
      cin >> theString;
   }
   while (theString.length() < 4);

   return theString;
}

//process inputs and send back to main.
string maskLetter(string theString, char keyLetter)
{
   getKeyLetter();
   keyLetter = '-';

   return keyLetter + theString;
}
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

// method prototypes
char getKeyLetter();
string getstring();
string maskLetter(string theString, char keyLetter);
string removeLetter();
int countKey();

int main()
{
  char keyLetter;
  string theString;

  theString = getstring();
  keyLetter = getKeyLetter();
 
  string maskedString = maskLetter(theString, keyLetter);

  cout << "String with '" << keyLetter << "' masked:\n";
  cout << maskedString << "\n\n";

  system("pause");
  return 0;
}

// gets inputs
char getKeyLetter()
{
  string letter = "PLACEHOLDER";
  char keyLetter;
  while (letter.length() != 1)
  {
    cout << "Please enter a SINGLE letter to act as key:";
    cin >> letter;
  }

  keyLetter = letter[0];
  return keyLetter;
}

string getstring()
{
  string theString;
  do 
  {
    cout << "Please enter a phrase or sentence >= 4: \n";
    cin >> theString;
  }
  while (theString.length() < 4);

  return theString;
}

//process inputs and send back to main.
string maskLetter(string theString, char keyLetter)
{
  string retval;

  for (size_t i = 0; i < theString.length(); i++)
  {
    if (theString[i] == keyLetter)
      retval += '-';
    else
      retval += theString[i];
  }

  return retval;
}
Topic archived. No new replies allowed.