# Thread: How do you allocate numbers to letters?

1. ## How do you allocate numbers to letters?

How would you allocate a number to every letter of the alphabet. Like, if you allocated 1 to a, 2 to b, 3 to c...and they typed in "abc", the program could output "123". I hope you understand, if you don't, just post your question in this thread, and I will give you the information that you want in order to answer my question.

Thanks
-Chris 2. Look into enumeration. You will also have to create a table of pointers to strings. Combine these two concepts. If you have a book than under the chapter on enumeration there will probably be an example.

I guess if afterward you are totally stuck than I could give an example partly borrowed from one of my textbooks but this would take me at least half an hour. Try to find some information first because there must be some examples out there. 3. ok. I'll try to find out all about that. If i'm still having problems i'll post another reply in this thread.

Thanks 4. you could try a switch statement:

Code:
```char input;
int i;
for(i = 0; i < 4; i++)
{
switch (input[i])
{
case 'a':
cout << "1" << endl;
break;
case 'b':
cout << "2" << endl;
break;
case 'c':
cout << "3" << endl;
break;
}
}``` 5. If you're just looking for some kind of letter transposition then this works well.

Code:
```#include <stdio.h>
#include <string.h>

int main ( ) {
char from[] = "abc";
char to[]   = "123";
char message[] = "abcdcba";
int i;
for ( i = 0 ; i < strlen(message) ; i++ ) {
char *pos = strchr( from, message[i] );
if ( pos != NULL ) {
printf( "%c", to[pos-from] );
} else {
printf( "?" );
}
}
printf( "\n" );
return 0;
}``` 6. ## yo... look @ ASCII

in ASCII theres ALLREADY a set enumeration for every charachter... you simply -48 for numbers to be numeric from ascii, and -97 for charahcters to be charachters from 0-23:

Code example:

Code:
```#include <stdio.h>
#include <string.h>

#define ASCIISETM 97

const char abcism = "abcdefg\0";

void main()
{
for( int sloop = 0 ; sloop<strlen(abcism) ; sloop++ )
{
printf("%i ",abcism[sloop]-ASCIISETM);
}
}```
SPH       7. ok...well, what I wanted to was how to do it using a string and for every letter (not just a, b and c. That was only an example).

Thanks
-Chris 8. ## so just change the string...

Just change the string from abcdef\0 to something else...

SPH 9. i still don't get it...anybody got an explanation....please....??? 10. You could use STL map -

Code:
```//Remove if you're not using MSVC
#pragma warning( disable:4786)

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
string a= "Ten";
string b= "Five Hundred";
int c=10;
int d=500;

map<int,string> mymap;

mymap[c]=a;
mymap[d]=b;

cout << mymap[d];

return 0;
}``` Popular pages Recent additions 