function to decrypted password

Hi!
I need to improve this code to insert the "0" before and after vowels with utilizinfg of string vowels.

string decryptPassword(string password)
{
const int ROT7 = 7, ROT9 = 9;
const string VOWELS = "AEIOUYaeiouy";
string decryptedpass;

for (size_t i = 0; i < password.length(); i++) {
if (even(i)) {
decryptedpass += password[i] + ROT7;
}
else {
decryptedpass += password[i] + ROT9;
}
decryptedpass += '0';
}
return decryptedpass;
}
Hello SEMOEY1990,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


The answer is 42.

https://www.youtube.com/watch?v=aboZctrHfK8
https://www.dictionary.com/e/slang/42/

Your code poses more questions than answers.

What is the value of password when the function receives it?

What is even(i) and how is used?

Given a "password" received by the function what should it look like when decrypted?

When using the {}s pick a style and be consistent in it use.
https://en.wikipedia.org/wiki/Indentation_style#Brace_placement_in_compound_statements

I find the "Allman" style the easiest to read and use. And that is a big goal to make the code easy to read. Also blank lines help.

As an example your code rearranged:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
string decryptPassword(string password)
{
	const int ROT7 = 7, ROT9 = 9;
	const string VOWELS = "AEIOUYaeiouy";

	string decryptedpass;

	for (size_t i = 0; i < password.length(); i++)
	{
		if (even(i))
		{
			decryptedpass += password[i] + ROT7;
		}
		else
		{
			decryptedpass += password[i] + ROT9;
		}

		decryptedpass += '0';
	}

	return decryptedpass;
}

Eventually you will notice that the code posted here tends to exaggerate the indenting.

Most of the time it is best to post enough code that can be compiled and tested. There is no point trying to write the missing code just to test the function because I have no idea what you did or what to expect when the function runs.

Andy
Hi Handy!
Im sorry because that I was not enough clear with my first post. I though that I dont need to write the whole program because I have a problem with this part of program. Any way I will rearrange my question.
You asked the exact same question a month ago:
https://www.cplusplus.com/forum/beginner/268431/
Yes, you have right. I tried to ask to the same post but it didnt work,I dont know why, I tried to reeplay at the same post but it didnt work so I made a new post instead.
Replace my comments with code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string
decryptPassword(string password)
{
    const int ROT7 = 7, ROT9 = 9;
    const string VOWELS = "AEIOUYaeiouy";
    string decryptedpass;

    for (size_t i = 0; i < password.length(); i++) {
        // If it's a vowel, then append '0' to to decryptedpass
        if (even(i)) {
            decryptedpass += password[i] + ROT7;
        } else {
            decryptedpass += password[i] + ROT9;
        }
        // If it's a vowel, then append '0' to to decryptedpass again
    }
    return decryptedpass;
}


Hi!
I wrote the following code but Im not sure if is correct. Then I reckon that this code doesnt add "0" before and after every vowel.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{string decryptPassword(string password)

    const int ROT7 = 7, ROT9 = 9;
    const string VOWELS = "AEIOUYaeiouy";
    string decryptedpass;

    for (size_t i = 0; i < password.length(); i++) {
        // If it's a vowel, then append '0' to to decryptedpass
        if (password[i] = VOWELS[0])
            password[i] += '0';
        if (even(i)) {
            decryptedpass += password[i] + ROT7;
        }
        else {
            decryptedpass += password[i] + ROT9;
        }
        if (password[i] = VOWELS[0])
            password[i] += '0';
        // If it's a vowel, then append '0' to to decryptedpass again
    }
    return decryptedpass;
}

Last edited on
Seems similar to this thread, might help: https://www.cplusplus.com/forum/beginner/266110/

password[i] += '0';
This is incrementing character, password[i], by the value of '0' itself. Not adding character onto the string, if that's what you're intending.
Last edited on
To tell if the letter is a vowel, you want to know if it's contained within the VOWELS string. You can do that by attempting to find the letter VOWELS. If it's found then it's a vowel. Here is a reference to the find method:
http://www.cplusplus.com/reference/string/string/find/
I have written this code but i dont know if its right
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string decryptPassword(const string& pass) {
    const size_t ROT7 = 7, ROT9 = 9;
    const string VOWELS = "AEIOUYaeiouy";

    stringstream ss;
    for (size_t i = 0; i < pass.size(); i++) {
        const size_t KEY = (i % 2 == 0) ? ROT7 : ROT9;
        char currentChar = pass[i], decryptedChar = currentChar + KEY;

        // determine whether current char is a vowel
        size_t found = VOWELS.find(VOWELS);
         if (currentChar = found)
    

        // if we have a vowel, add '0' before and after decryptedChar
             decryptedChar+='0';
    }

    return ss.str();
}
Last edited on
Well, the short version is that it's not right.
But we won't always be able to tell you that.

How would you determine if it's right or not? You have to use the function in a series of tests.
input --> expected output.
Does the given input produce the expected output? Start with simple examples to see if they work.

Some specific issues

1.
1
2
       // if we have a vowel, add '0' before and after decryptedChar
             decryptedChar+='0';
This change doesn't affect anything, because decryptedChar isn't being used to do anything. It's simply a local char variable that you have declared.
It also isn't doing what you intend, because you're adding to the value of the char itself. A char is not a string. A string is made up of multiple chars.

Here is how you prepend '0' to a string:
1
2
string str = "blah";
str = "0" + str;


2.
1
2
        size_t found = VOWELS.find(VOWELS);
         if (currentChar = found)

= is for assignment, not testing for equality. == tests for equality.
But this is not how string.find is used.
Check out https://www.geeksforgeeks.org/string-find-in-cpp/ for some examples,
1
2
3
4
5
6
7
    string str = "geeksforgeeks a computer science"; 
    string str1 = "geeks"; 
  
    // Find first occurrence of "geeks" 
    size_t found = str.find(str1); 
    if (found != string::npos) 
        cout << "First occurrence is " << found << endl; 


3.
You return ss.str(), but where do you ever add anything to ss?
Perhaps this may guide you:
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
// Example program
#include <iostream>
#include <string>
using std::string;
using std::cout;

bool is_vowel(char ch)
{
    string vowels = "aeiouAEIOU";
    return (vowels.find(ch) != string::npos);
}

string pad_vowels_with_0(const string& str)
{
    string output;
    
    bool previous_char_was_vowel = false;
    
    for (size_t i = 0; i < str.length(); i++)
    {
        char ch = str[i];
        if (is_vowel(ch))
        {
            if (previous_char_was_vowel)
            {
                // only append to right-hand side 
                output += ch;
                output += '0';
            }
            else
            {
                // append both before and after vowel
                output += '0';
                output += ch;
                output += '0';
            }
            
            previous_char_was_vowel = true;
        }
        else
        {
            output += ch; // just add the char, no 0's
            previous_char_was_vowel = false;   
        } 
    }
    
    return output;
}

int main()
{
    {
        string input = "xyz"; 
        string output = pad_vowels_with_0(input);
        cout << input << " --> " << output << '\n';
    }
    {
        string input = "ae"; 
        string output = pad_vowels_with_0(input);
        cout << input << " --> " << output << '\n';
    }
    {
        string input = "blahoi"; 
        string output = pad_vowels_with_0(input);
        cout << input << " --> " << output << '\n';
    }
}


Note the "previous_char_was_vowel" flag lets "ae" become "0a0e0" instead of "0a00e0".
Last edited on
I would like to thank you first for your explanation. Then excuse me that I write a little sloppy . I am a completely beginner to programming and this task is over my knowledge so I have a lot of difficulty understanding everything, I read our lectures but it didn't help much. Anyway, thank you for your help and it would be good if you send several links that can help me to be wiser.
Thanks for example, it was really educative.
Topic archived. No new replies allowed.