Replacing Characters in a String (still need help as of 20th)

Relatively new to C++. I posted a previous question related to this before, and have since continued to work on the problem, to no avail.

Basically, I need a function that takes a string as input, then replaces the letters of the string based on another function.

This other function uses a string to define the characters that may be used in the input,

 
const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,' ";


then creates a permutation by randomly reorganising the letters.

1
2
3
4
5
6
7
8
string create_permutation(string permutation)
{
	unsigned seed = chrono::system_clock::now().time_since_epoch().count();
	shuffle(permutation.begin(), permutation.end(), default_random_engine(seed));
	return permutation;
}

string permu = create_permutation(ALPHABET);


How do I essentially encrypt the message input, by looking up the alphabet letter, and then replacing the input letter with the corresponding permutation letter?

E.G.

Input is 'HELLO'
const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,' ";
permutation of ALPHABET = "BUYWPLJDKIXQSZGAVCEFN'MR,OT .H";
Output should be 'DPFFG' (D replaces H's place, P replaces E's place, etc)

Below is the full code used. Note that the inverse permutation would then be used to decrypt the encrypted message.

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

using namespace std;

const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,' ";

string create_permutation(string permutation)
{
	cout << "Input seed" << endl;
	unsigned seed;
	cin >> seed;
	shuffle(permutation.begin(), permutation.end(), default_random_engine(seed));
	return permutation;
}

string findPermu2(const string& permu1)
{
	string permu2(ALPHABET);
	for (int i = 0; i<ALPHABET.length(); ++i)
		for (int j = 0; j<ALPHABET.length(); ++j)
			if (permu1[i] == ALPHABET[j])
			{
				permu2[j] = ALPHABET[i];
				break;
			}
	return permu2;
} 

int main()
{
	string permu = create_permutation(ALPHABET);
	string invpermu = findPermu2(permu);

	cout << "Original ALPHABET:        - " << ALPHABET << endl;
	cout << "Permutation of ALPHABET:  - " << permu << endl;
	cout << "Inverse of Permutation:   - " << invpermu << endl;
	cout << endl << "Input any single character to begin" <<
		endl << "Encryption / decryption, or '\Q'\ to [Q]uit" << endl;

	char enterorquit;
	cin >> enterorquit;
	if (enterorquit == 'q' || enterorquit == 'Q')
	{
		return 0;
	}
	else
	{
		string inputmessage;
		cout << "Type your message below" << endl;
		cin >> inputmessage;
		cout << endl << "Input \'E\' to [E]ncrypt, any other single character to decrypt:" << endl;
	}
	char encryptordecrypt;
	if (encryptordecrypt == 'e' || encryptordecrypt == 'E')
		{
			cout << "Encrypted message: " << endl /*<< encrypted message here*/;
		}
		else
		{
			cout << "Decrypted message: " << endl /*<< decrypted message here*/;
		}
					return 0;
}
Last edited on
foo = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,' ";
bar = "BUYWPLJDKIXQSZGAVCEFN'MR,OT .H";

At what position x is 'H' in foo? 7
What is at position x in bar? bar[7] == 'D'
That makes sense, but how would that be used to change a string's letters?

Something like this?

foo = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,' ";
bar = "BUYWPLJDKIXQSZGAVCEFN'MR,OT .H";
input = 'HELLO'

for (i = 0, i<input.length, ++i)
{
/* something to represent input[i] being replaced by (foo[i] == bar[i]) */
}
In a loop over the input array, use
int k = strchr( foo, input[j] );
to find the index of the replacement, and then set
input[j] = bar[k];
But be sure to check that k is >= 0 and decide what to do if it isn't (i.e., if the jth character of input is not in the foo array)
Is this correct?

1
2
3
4
5
6
7
8
9
10
11
12
for (int i = 0; i < inputmessage.length; ++i)
			{
				int j;
				if (int k = strchr(ALPHABET, inputmessage[j]))
				{
					inputmessage[j] = permu[k];
				}	
				else 
				{
					cout << "Invalid characters detected in string" << endl;
				}
			}


strchr is stating an error - no instance of overloaded function "strchr" matches the argument list
argument types are: (const std::string, char)
Still need assistance, if anyone could lend a hand.
closed account (D80DSL3A)
The letter j in fredk's example corresponds to your letter i.
He said check if k>=0. That's what goes in the if().
The function strchr() needs to find the index in ALPHABET where the letter inputmessage[j] appears.
I don't know this function, but the findEle() function from the other thread does just this. Case 3 alone is fine, just brute force it.
1
2
3
4
5
6
7
8
9
10
11
12
for (int i = 0; i < inputmessage.length(); ++i)
{
	int k = strchr(ALPHABET, inputmessage[j]);
	if ( k >= 0 )
	{
		inputmessage[i] = permu[k];
	}	
	else 
	{
		cout << "Invalid characters detected in string" << endl;
	}
}
Last edited on
Strchr is again producing the same error message.

Error: no instance of overloaded function "strchr" matches the argument list
argument types are: (const std::string, char)

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
if (enterorquit == 'q' || enterorquit == 'Q')
	{
		return 0;
	}
	else
	{
		string inputmessage;
		cout << "Type your message below" << endl;
		cin >> inputmessage;
		cout << endl << "Input \'E\' to [E]ncrypt, any other single character to decrypt:" << endl;
		char encryptordecrypt;
		cin >> encryptordecrypt;
		if (encryptordecrypt == 'e' || encryptordecrypt == 'E')
		{
			for (int i = 0; i < inputmessage.length; ++i)
			{
				int k = strchr(ALPHABET, inputmessage[i]);
				if ( k >= 0 )
				{
					inputmessage[i] = permu[k];
				}	
				else 
				{
					cout << "Invalid characters detected in string" << endl;
				}
			}
			cout << "Encrypted message: " << endl << inputmessage;
		}
		else
		{
			cout << "Decrypted message: " << endl /*<< decrypted message here*/;
		}
	}
		return 0;
}
Here is the function strchr: http://www.cplusplus.com/reference/cstring/strchr/

Take a look at what input parameters it accepts. A char-pointer, and an int.

Take a look at how you're trying to use it: strchr(ALPHABET, inputmessage[i]);

ALPHABET is an object of type string. Is a string an object of type char-pointer? No, it is not.

Understand the functions you're trying to use. Look at the function documentation, see what kinds of parameters it accepts, and write code accordingly.
Last edited on
Thanks, I understand why the error is showing now. I wonder if there is some way to manipulate ALPHABET so that it would be acceptable with strchr? Or if not, if you've encountered another function that would be better suited.
You do "wonder" what operations does a std::string support?

Read: http://www.cplusplus.com/reference/string/string/


@fredk: What made you suggest the strchr (a C function)? Did you notice that the ALPHABET is a std::string?
@keskiverto: I missed the fact that ALPHABET was a string. The OP can use ALPHABET.find_first_of(c,0) to find the index, if it exists.
Topic archived. No new replies allowed.