Permutation of String

Mar 30, 2016 at 12:24pm
closed account (jT59E3v7)
Hi,

I have been set a task to create an enigma machine in C++. I am currently stuck on a question that is as follows:

'Write a function that takes a string as input and returns true if this string is a permutation of the alphabet and false otherwise.'

The alphabet in this case is a string set to: "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,’ " (please exclude quotes)

I have written the following code which should return 1 if the inputted string is the same length as the alphabet, and if the string is a permutation. My code does not take into account the is_permutation and seems to return 0 regardless. I was wondering if I have missed something out or if there is a better way to approach this?

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

int permutationCheck(string input) {
const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,’ ";


if (ALPHABET.length() == input.length()) {
bool flag[29] = { 0 };
if (is_permutation(ALPHABET.begin(), ALPHABET.end(), input.begin())) {

cout << "This is true" << endl;

return 1;
}

else {
return 0;
}
}
else {
return 0;
}
}

int main()
{string input;

cout << "Enter a permutation of the alphabet" << endl;
getline(cin, input);
cout << permutationCheck(input) << endl;
system("pause");
return 0;
}
Mar 30, 2016 at 1:42pm
Please use [code] tags.

You're making this more complicated than it needs to be, I think. Your function could be simply something like:
1
2
3
4
int permutationCheck(string input) {
    const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,’ ";
    return is_permutation(ALPHABET.begin(), ALPHABET.end(), input.begin()) ? 1 : 0;
}


If you still have strange output, I'd use a debugger to make sure the values of variables are what you expect to be when you call your function.
Mar 30, 2016 at 2:07pm
closed account (jT59E3v7)
Hi,

Thanks for your reply, I have tested out the revised function, however I seem to get a 0 for all scenarios, even when the input is not a valid permutation.

I also seem to get a debug assertion error in Visual Studio when the length of the input does not match the alphabet string, is this normal?

Thanks

M
Mar 30, 2016 at 2:12pm
Could you post your input? The code worked unmodified for me except for the input string.

If you add some trace to the else blocks, you'll have a better idea where things go wrong (see example below).

Anyhow your string ALPHABET may not be what you think. The next to last character that looks like the ASCII character for an apostrophe is actually the right quote unicode 209!

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 <limits>
...
bool permutationCheck(const string &input) 
{
	const string ALPHABET("ABCDEFGHIJKLMNOPQRSTUVWXYZ.,’ ");
	if (ALPHABET.length() == input.length()) 
	{
		if (is_permutation(ALPHABET.begin(), ALPHABET.end(), input.begin())) 
		{
			cout << "This is true" << endl;
			return (true);
		}
		else 
		{
			cout << __LINE__ << ": is_permutaion() returned false." << endl;
			return (false);
		}
	}
	else 
	{
		cout << __LINE__ << ": string lengths are not equal." << endl;
		return (false);
	}
}


Try displaying the numerical values of the characters in the strings and it'll be clear that there are differences.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
static inline void ip(const string &input)
{
	for (auto c : input)
		cout << hex << (int)c << " ";
	cout << dec << endl;
}

bool permutationCheck(const string &input) 
{
	const string ALPHABET("ABCDEFGHIJKLMNOPQRSTUVWXYZ.,’ ");
	ip(ALPHABET);
	ip(input);
	...
Mar 30, 2016 at 2:12pm
Have you opened the program in a debugger and looked at the values of input and ALPHABET to compare to each other? I'm guessing they actually aren't permutations but you think they are.

You are right, I had been thinking of the C++14 version of is_permutation() that allows the specification of a start and end for both strings used in the comparison. You can either use that one or re-add the length check you had before.
Mar 30, 2016 at 2:20pm
closed account (jT59E3v7)
Hi,

Thanks for your help. You were both right about the two strings not matching when I thought they were, the right quote(Unicode 209) was being mistaken for another character. This was made clear when the values were compared. The program is now working as expected

Thanks for your help

M
Topic archived. No new replies allowed.