473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to convert alphabet to numbers?

4
im newbie here.. n need some help..

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

example: a = 01
Feb 10 '08 #1
12 86811
Laharl
849 Expert 512MB
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.

Expand|Select|Wrap|Line Numbers
  1. const char* alpha = "abcdefghilmnopqrstuvwxyz"; //Zero-indexed array of letters
  2.  
  3. char b = 'b'-'a'+1; //b=2, an unprintable character
  4. int c = b; //b=2
  5.  
  6.  
Feb 10 '08 #2
aRiFiN
4
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...
Feb 10 '08 #3
Laharl
849 Expert 512MB
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.
Feb 10 '08 #4
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

Expand|Select|Wrap|Line Numbers
  1.     char arr[255];
  2.     int valArr[255],index;
  3.     printf("Enter string: ");
  4.     scanf("%s",arr);
  5.     for(index=0;index<strlen(arr);++index){
  6.         if(arr[index]>='a' && arr[index]<='z'){
  7.             valArr[index] = arr[index]-'a'+1;
  8.         }
  9.     }
  10.     for(index=0;index<strlen(arr);++index){
  11.         printf("%02d",valArr[index]);
  12.     }
  13.  
Feb 11 '08 #5
aRiFiN
4
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
Feb 26 '08 #6
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<<endl;
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
Feb 26 '08 #7
Banfa
9,065 Expert Mod 8TB
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.
Feb 26 '08 #8
aRiFiN
4
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. main() {
  5.  
  6.           char arr[255];
  7.  
  8.           int valArr[255],index;
  9.  
  10.       clrscr();
  11.  
  12.           printf("Enter string: ");
  13.  
  14.           gets(arr);
  15.  
  16.           for(index=0;index<strlen(arr);++index){
  17.  
  18.             if(arr[index]>='a' && arr[index]<='z'){
  19.  
  20.                   valArr[index] = arr[index]-'a'+1;
  21.  
  22.               }
  23.         else
  24.         {
  25.             if (isspace(arr[index]))
  26.             {
  27.                 valArr[index] = '\0';
  28.             }
  29.         }
  30.           }
  31.  
  32.           for(index=0;index<strlen(arr);++index){
  33.  
  34.               printf("%02d",valArr[index]);
  35.  
  36.           }
  37. getch();
  38. }
  39.  
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?
Mar 11 '08 #9
Expand|Select|Wrap|Line Numbers
  1. 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'};
  2. #include<stdio.h>
  3. main()
  4. {
  5. int i,j,size;
  6. char srt[100];
  7. clrscr();
  8. printf("enter your string size");
  9. scanf("%d",&size);
  10. printf("enter your name or a string");
  11. for(i=0;i<=size;i++)
  12. {
  13. scanf("%c",&str[i]);
  14. }
  15.             for(i=0;i<=size;i++)
  16.              {
  17.               for(j=0;j<26;j++)
  18.                 {
  19.                  if(str[i]==a[j])
  20.                  {
  21.                   printf("\t%d",j+1);
  22.                   }
  23.                  }
  24.               }
  25. }
  26.  
  27. \* This program defnetly works.
  28.     o/p: enter your string size : 50
  29.          enter your name or a string : john abraham
  30.          10 15 8 14 1 2 18 1 8 1 13
Aug 4 '14 #10
did my programs works..?
does that gives u exact result what u want..?
please comment...
Sep 23 '14 #11
zmbd
5,501 Expert Mod 4TB
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.
Oct 6 '14 #12
It is helpful but there is some problem in your explanation . The ascii code of 'a' is 97
Nov 7 '17 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

30
by: rh0dium | last post by:
Hi All, While I know there is a zillion ways to do this.. What is the most efficient ( in terms of lines of code ) do simply do this. a=1, b=2, c=3 ... z=26 Now if we really want some...
5
by: PCV | last post by:
Hi All, I want to create a custom auto number in a form that looks like "CRA05001" where "CR" are fixed characters, then "A" should be for January as "B" for February and so on...(this part will...
4
by: JIM.H. | last post by:
Hello, string newStr=myStr.Substring(1,3); int newNum = System.Convert.ToInt32(newStr); This fails if newStr does not have all characters as numbers. How can you do this in a professional...
1
by: mktilu | last post by:
do we have any function to convert the numbers into alphabet in vb 6.For example 25 to be converted to twenty Five. Its urgent
1
by: Evando | last post by:
I need a possible solution to tackle my assignment.The instruction again is to write a program code in C to convert from Roman numbers into Decimals.Any possible solution which will run is needed. ...
3
by: PulkitZery | last post by:
Hi all, I need some help in my project (VB.NET) here is what I need: I need to convert Alphabetic number (a. b. c. d. …..) into the integers (for example a=1, b=2, c=3 and so on). Can anyone...
2
by: sandeep2 | last post by:
how to convert the numbers into words in asp.net using c#,please help me,its very urgent....?
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.