Help

Can you help me to solve this?

"Create a program that allows to type a old password then it should creates a new password. All vowel letter should Replace with letter X and all numbers should replace with letter Z. New password must be done in reversed".

I have a code of this but I dont know how to reverse.

#include<iostream>
#include<string>
using namespace std;


int main()

{
long loop = 0;
int k = 0;
int count = 0;

while (loop>-1)

{
string input;
cout << "Enter Original Password (Enter -1 to end): ";
getline(cin, input);


if (input == "-1")
{
break;
}
count = input.length();


for (k = 0; k < count; k++)
{
if (input[k] == 'a' || input[k] == 'A' || input[k] == 'e' || input[k] == 'E' || input[k] == 'i' || input[k] == 'I' || input[k] == 'o' || input[k] == 'O' || input[k] == 'u' || input[k] == 'U')
{
input.replace(k, 1, "X");
}
if (input[k] == '1' || input[k] == '2' || input[k] == '3' || input[k] == '4' || input[k] == '5' || input[k] == '6' || input[k] == '7' || input[k] == '8' || input[k] == '9' || input[k] == '0')
{
input.replace(k, 1, "Z");
}



}
cout << "Your New Password:" << input << endl;


}
system("pause");

}



The output should be this:

Enter Original Password (Enter -1 to end): A1 qw2
Your New Password: Zwq ZX
Enter Original Password (Enter -1 to end): aap2
Your New Password: ZpXX
Enter Original Password (Enter -1 to end): -1
Press any key to continue...
Last edited on
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
#include <iostream>
#include <string>
#include <cctype> // std::isdigit
#include <algorithm> // std::reverse

int main()
{
    const std::string vowels = "AEIOUaeiou" ;

    std::string original_pword ;

    while( std::cout << "Enter Original Password (Enter -1 to end): " &&
           std::getline( std::cin, original_pword ) && original_pword != "-1" )
    {
        std::string new_pword ;

        // http://www.stroustrup.com/C++11FAQ.html#for
        for( char c : original_pword ) // for each char in the original password
        {
            // if it is a vowel (if it is a character in the vowels string), add an 'X'
            // https://en.cppreference.com/w/cpp/string/basic_string/find
            if( vowels.find(c) != std::string::npos ) new_pword += 'X' ;

            // otherwise, if it is a digit, add a 'Z'
            // https://en.cppreference.com/w/cpp/string/byte/isdigit
            else if( std::isdigit(c) ) new_pword += 'Z' ;

            // otherwise, add the character as it is
            else  new_pword += c ;
        }

        // reverse the new password https://en.cppreference.com/w/cpp/algorithm/reverse
        std::reverse( new_pword.begin(), new_pword.end() ) ;

        std::cout << "your new password is: '" << new_pword << "'\n" ;
    }
}
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 <cctype>
#include <string>
#include <algorithm>
using namespace std;

string newPassword( string pwd )
{
   const string vowels = "AEIOUaeiou";
   reverse( pwd.begin(), pwd.end() );
   for ( char &c : pwd )
   {
      if ( vowels.find( c ) != string::npos ) c = 'X';
      else if ( isdigit( c ) ) c = 'Z';
   }
   return pwd;
}

int main()
{
   string pwd;
   cout << "Enter your old password: ";   cin >> pwd;
   cout << "Your new password is " << newPassword( pwd );
}
Last edited on
Topic archived. No new replies allowed.