Input a name and convert it to an encrypted name

using string as a data type, how to make a program where you input a name and then coverts the letters a to @, e to ^, i to !, o to * and u to $ regardless if it is upper or lower case.

I need to first input the name and the program will change the letters.

this is the sample output:

Enter a name: Noah U. Collymore
Encrypted: N*@h $. C*llym*r^
Last edited on
This is not encryption, more a very simple form of obfuscation!

Anyway, you can do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void obfuscate_string(char *str)
{
    for (; *str; ++str)
    {
        switch (tolower(*str))
        {
        case 'a':
            *str = '@';
            break;        
        case 'e':
            *str = '^';
            break;        
        /* ... */
        }
    }
}


...or if you have an std::string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void obfuscate_string(std::string &str)
{
    for (std::string::iterator it = str.begin(); it != str.end(); ++it)
    {
        switch (std::tolower(*it))
        {
        case 'a':
            *it = '@';
            break;        
        case 'e':
            *it = '^';
            break;
        /* ... */
        }
    }
}


If you have a large number of mappings, then you could also use a lookup-table or std::map, so that you don't have to write a case for each mapping. But I think that's overkill for your simple example...
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
#include <iostream>
#include <cstring>

std::string obfuscate(std::string str) {
	static constexpr const char* from {"aeiouAEIOU"};
	static constexpr const char* to {"@^!*$@^!*$"};

	for (auto& ch : str)
		if (const auto fnd {strchr(from, ch)}; fnd != nullptr)
			ch = to[fnd - from];

	return str;
}

int main() {
	const std::string str {"Noah U. Collymore"};

	std::cout << obfuscate(str) << '\n';
}



N*@h $. C*llym*r^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;

string encode( string str )
{
   const string vowels = "aeiouAEIOU", slewov = "@^!*$";
   for ( char &c : str )
   {
      int p = vowels.find( c );
      if ( p != string::npos ) c = slewov[p%5];
   }
   return str;
}

int main()
{
   cout << encode( "Noah U. Collymore" ) << '\n';
}
I made this awhile ago. It works correctly but I forget how it works and I wrote it in a way that was intentionally deceptive. I've even looked at it after the fact and have a rough time understanding how it works. I think it was dependent on other code in a different part of the program to work correctly but it may give you some ideas.
https://pastebin.com/f4JNsmEV
Last edited on
What if I need to first input the name and the program will change the letters?

something like this:
Enter a name: _________
Encrypted name: _________
Last edited on
I need to first input the name and the program will encrypt it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>
#include <iostream>
#include <cstring>

std::string obfuscate(std::string str) {
	static constexpr const char* from {"aeiouAEIOU"};
	static constexpr const char* to {"@^!*$@^!*$"};

	for (auto& ch : str)
		if (const auto fnd {strchr(from, ch)}; fnd != nullptr)
			ch = to[fnd - from];

	return str;
}

int main() {
	std::string str;

	std::cout << "Enter a name: ";
	std::getline(std::cin, str);

	std::cout << "Encrypted name: " << obfuscate(str) << '\n';
}

Topic archived. No new replies allowed.