Replace numbers in a string

Hello, I am attempting a program that gathers a password from the user, and then creates and displays a new password, given the following rules: All vowels in the original password should be replaced with 'X', All of the characters should be reversed, and all numbers should be replaced with the letter 'Z'. I am close to the program, but I can't seem to figure out how to replace all of the numbers without making a bunch of if statements that seems very inefficient. Any help would be much appreciated.

/* Joe ; C++
*/
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main()
{
string password = "";
string newPass = "";

cout << "Enter password (-1 to stop): " << endl;
getline(cin, password);

cout << "New Password: ";
for (int y = 0; password[y] != '\0'; y++)
{
switch (password[y])
{
case 'a': password[y] = 'X';
break;
case 'e': password[y] = 'X';
break;
case 'i': password[y] = 'X';
break;
case 'o': password[y] = 'X';
break;
case 'u': password[y] = 'X';
break;
}//end switch
}//end for

for (int x = password.length() - 1; x >=0; x--)
{
cout << password[x];
}
system("pause");
return 0;
}//end of main
This doesn't account for spaces.....but this should be what you were after

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
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    // All vowels in the original password should be replaced with 'X', 
    // All of the characters should be reversed, and all numbers should 
    //be replaced with the letter 'Z'.
    string pw;
    int vCheck;
    int numCheck;
    string xVowels = "aeiou";
    string zNumbers = "1234567890";


    
    cout<<"Please enter password" <<endl;
    getline(cin,pw);
    
    cout<<"The stuff you entered is: " <<pw <<endl;
    cout<<"The string is " <<pw.length() <<" characters long!" <<endl;
    
    //Reverse pw
    reverse(pw.begin(), pw.end());
    cout <<"reversed " <<pw <<endl;
    
    // Changes all vowels to X, Changes all # to Z
    for (int y = 0; y <= pw.length()-1; ++y){
        for (int a = 0; a <=xVowels.length()-1; ++a){
            vCheck = xVowels.at(a);
            if (pw.at(y) == xVowels.at(a)){
                pw.at(y) = 'x';
            }
            if (pw.at(y) == zNumbers.at(a)){
                pw.at(y) = 'Z';
            }
        }
    }
    cout <<"New PW with reverse and replaced stuff = " <<pw <<endl;
    return 0;
}
The change functionality can be simplified:
1
2
3
4
5
6
7
8
9
10
11
12
// Changes all vowels to X, Changes all # to Z
  for (char& ch : pw) // for each char in pw
  {
    if (xVowels.find(ch) != string::npos)
    {
      ch = 'X';
    }
    else if (zNumbers.find(ch) != string::npos) // or use isdigit from <ctype>
    {
      ch = 'Z';
    }
  }
Your vowel list also needs the upper-case equivalents.
Topic archived. No new replies allowed.