How to convert alphabet to numbers?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aRiFiN
    New Member
    • Feb 2008
    • 4

    How to convert alphabet to numbers?

    im newbie here.. n need some help..

    alphabets got 26 character n each character got secret number..

    example: a = 01
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Characters are already directly convertible to integers via the ASCII standard, http://asciitable.com/ has the conversions. To quickly get a->1 and such, subtract 'a' from a given character and add 1.

    You can also use an array of all the characters, which would make a->00 and z->25, since arrays are zero-indexed. To get a->01, just pad the array with some control character that won't be used, maybe a number.

    If you're after some sort of substitution encryption, either of these solutions will be fairly easy to work with, as the latter can easily be declared in global scope and the former allows direct manipulation.

    [CODE=cpp]
    const char* alpha = "abcdefghilmnop qrstuvwxyz"; //Zero-indexed array of letters

    char b = 'b'-'a'+1; //b=2, an unprintable character
    int c = b; //b=2

    [/CODE]

    Comment

    • aRiFiN
      New Member
      • Feb 2008
      • 4

      #3
      i know about ASCII standard but can u teach me how to apply ASCII code in turbo c.

      if a=01,b=02,c=03

      when i type "abc" the answer should be 010203

      i almost giv up doing this.. huhuh...

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        If you want a direct way to link a->01, use a std::map<char, int>. That might fit your needs a little better, as it allows you to easily change the number a given character is mapped to.

        Comment

        • keerthiramanarayan
          New Member
          • Nov 2007
          • 13

          #5
          If you know the ASCII standard then you know that 'a' has value 65. Now if you want to print out your code for 'a' as 01 then you have to subtract your character first by 65 and then add 1. effectively code of 'b' is ASCII(b) - ASCII(a) +1,
          ASCII(b) = 66
          ASCII(a) = 65

          Therefore ASCII(b) - ASCII(a) +1 = 2.

          Next you get to coding. The following snippet considers that the value for the character is only between 'a' and 'z' and this can be easily extended

          [CODE="cpp"]
          char arr[255];
          int valArr[255],index;
          printf("Enter string: ");
          scanf("%s",arr) ;
          for(index=0;ind ex<strlen(arr); ++index){
          if(arr[index]>='a' && arr[index]<='z'){
          valArr[index] = arr[index]-'a'+1;
          }
          }
          for(index=0;ind ex<strlen(arr); ++index){
          printf("%02d",v alArr[index]);
          }
          [/CODE]

          Comment

          • aRiFiN
            New Member
            • Feb 2008
            • 4

            #6
            when i did space.. the answer was wrong.. but thanks bro... its help me soo much..

            example: when i enter the string a b c it should show me 01 02 03

            Comment

            • Simonius
              New Member
              • Feb 2008
              • 47

              #7
              a is 97 not 65, A is 65.

              And if you just want the ascii value then just cast it to an int.
              for example;

              char test='a';
              cout<<test<<end l;
              cout<<(int)test ;

              Has as output
              a
              97

              Here's an ascii table: http://www.asciitable.com/

              getting the values as you want them is easy if it's all lower letters or upper cases.

              If it's all lower cases deduct 96, upper case -64

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by Laharl
                Characters are already directly convertible to integers via the ASCII standard, http://asciitable.com/ has the conversions. To quickly get a->1 and such, subtract 'a' from a given character and add 1.
                Note this is only true if the execution character set happens to be ASCII (or another character set where the letters are contiguous). The C/C++ standards do not specify either the compilation or execution character sets which can be anything and there are some character sets where the letters are not contiguous.

                The C/C++ standards do specify that whatever character set is in use the character '0' - '9' must be contiguous, e.g. '5' - '0' == 5 is guaranteed by the standard for a conforming implementation of the compiler.

                Having said all that many (most?) compilers today do use the ASCII character set and many people do use this sort of character mathematics.

                However in doing so you should be aware that you have introduced a possible portability issue into your code.

                Here is an example of a character set that used to be used in compilers/computers a far bit but does not have contiguous letters.

                Comment

                • aRiFiN
                  New Member
                  • Feb 2008
                  • 4

                  #9
                  Code:
                  #include <stdio.h>
                  #include <ctype.h>
                  
                  main() {
                  
                            char arr[255];
                  
                            int valArr[255],index;
                  
                  	  clrscr();
                  
                            printf("Enter string: ");
                  
                            gets(arr);
                  
                            for(index=0;index<strlen(arr);++index){
                  
                  	        if(arr[index]>='a' && arr[index]<='z'){
                  
                                    valArr[index] = arr[index]-'a'+1;
                  
                                }
                  		else
                  		{
                  			if (isspace(arr[index]))
                  			{
                  				valArr[index] = '\0';
                  			}
                  		}
                            }
                  
                            for(index=0;index<strlen(arr);++index){
                  
                                printf("%02d",valArr[index]);
                  
                            }
                  getch();
                  }
                  when i change "scanf("%s",arr );" to "gets(arr)" and
                  i get the result:

                  Enter String: a bc
                  Output: 01000203

                  can i get the result as 01 0203?

                  Comment

                  • pridephani
                    New Member
                    • Aug 2014
                    • 2

                    #10
                    Code:
                    int a[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
                    #include<stdio.h>
                    main()
                    {
                    int i,j,size;
                    char srt[100];
                    clrscr();
                    printf("enter your string size");
                    scanf("%d",&size);
                    printf("enter your name or a string");
                    for(i=0;i<=size;i++)
                    {
                    scanf("%c",&str[i]);
                    }
                                for(i=0;i<=size;i++)
                                 {
                                  for(j=0;j<26;j++)
                                    {
                                     if(str[i]==a[j])
                                     {
                                      printf("\t%d",j+1);
                                      }
                                     }
                                  }
                    }
                    
                    \* This program defnetly works.
                        o/p: enter your string size : 50
                             enter your name or a string : john abraham
                             10 15 8 14 1 2 18 1 8 1 13
                    Last edited by Rabbit; Aug 4 '14, 03:38 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

                    Comment

                    • pridephani
                      New Member
                      • Aug 2014
                      • 2

                      #11
                      did my programs works..?
                      does that gives u exact result what u want..?
                      please comment...

                      Comment

                      • zmbd
                        Recognized Expert Moderator Expert
                        • Mar 2012
                        • 5501

                        #12
                        pridephani:
                        You posted to a long dead thread... it's over 6 years old. Either the poster has already solved the problem or isn't around any more to care.

                        Comment

                        • Tasnin Khan
                          New Member
                          • Nov 2017
                          • 1

                          #13
                          It is helpful but there is some problem in your explanation . The ascii code of 'a' is 97

                          Comment

                          Working...