assign letters to numbers

Say I have a vector of char that contains the letters: 'happy'
I want to assign a number to each one of these characters, but, those numbers should only be from 0 to 9.
Then I want to apply next_permutation() on those numbers.

There is one thing though: the word happy has 5 letters. So that means we will assign 0-4 to it right? No. I want to be able to assign up to 9. So from 0-9.

The reason I am doing this because I am solving a cryptarithmetic puzzle.

'are' + 'you' = 'happy'

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

int main()
{
	vector <char> words = { 's','e','n','d','m','o','r','y' };
	vector <int> digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	//iterate and give values
	for (vector<char>::iterator wordsIterator = words.begin(); wordsIterator != words.end(); ++wordsIterator)
	{

	}
	system("PAUSE");
	return 0;
}
Your question is honestly unclear. How do you want to "assign" one number to each character?

Edit: perhaps std::map would fit your needs better - http://www.cplusplus.com/reference/map/map/begin/
Last edited on
Ok let's say I want to use maps. Also, I want the user to input the string.

First I would ask the user to input a string, for example:

send + more = money

then I will extract the unique characters from this string:

s, e, n, d, m, o, r, y

I have programmed up to this point and it works fine.



Now I want to give each letter a unique number.

Next, I will use next_permutation() to give me all the possible permutations of those numbers.

I am going to try to do that, hopefully I can do it.

Edit:

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 <map>
#include <algorithm>
using namespace std;

int main()
{
	map <char, int> characters;
	characters['s'] = 0;
	characters['e'] = 1;
	characters['n'] = 2;
	characters['d'] = 3;
	characters['m'] = 4;
	characters['o'] = 5;
	characters['r'] = 6;
	characters['y'] = 7;
	do
	{
		cout << characters[0] << ' ' << characters[1] << ' ' << characters[2] << endl;
	} while (next_permutation(characters.begin(), characters.end()));

	system("PAUSE");
	return 0;
}


I got an error, "you cannot assign to a variable that is const"
Last edited on
Topic archived. No new replies allowed.