How do you allocate numbers to letters?

This is a discussion on How do you allocate numbers to letters? within the C++ Programming forums, part of the General Programming Boards category; How would you allocate a number to every letter of the alphabet. Like, if you allocated 1 to a, 2 ...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Thumbs up 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. #2
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    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.
    Last edited by Witch_King; 09-02-2001 at 02:51 AM.
    I compile code with:
    Visual Studio.NET beta2

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    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. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    you could try a switch statement:

    Code:
    char input[4];
    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. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    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;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    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[255] = "abcdefg\0";
    
    void main()
    {
           for( int sloop = 0 ; sloop<strlen(abcism) ; sloop++ )
          {
                printf("%i ",abcism[sloop]-ASCIISETM);
          }
    }
    SPH

  7. #7
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    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. #8
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Talking so just change the string...

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

    SPH

  9. #9
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    i still don't get it...anybody got an explanation....please....???

  10. #10
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    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;
    }
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to use case/switch with letters and numbers
    By jericjones45 in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 04:38 PM
  2. Count number of letters and numbers in a file
    By jtullo in forum C Programming
    Replies: 2
    Last Post: 04-21-2008, 01:33 AM
  3. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 09:54 PM
  4. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM
  5. Transforming letters to numbers...
    By N8760 in forum C++ Programming
    Replies: 2
    Last Post: 12-23-2001, 02:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21