473,594 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Translating alphabetic number in C

crystal2005
44 New Member
Hi guys,

Just like the title, i'm looking for the example of C program that translates an alphabetic phone number into numeric. The idea as the following output

Enter phone number: 1800-TEST-100

result: 1800-8378-100


so basically 2=ABC, 3=DEF, 4=GHI, . . . , 9 = WXYZ

So, all the non-alphabetic (E.g. dash, punctuations) will leave unchanged.

Can anyone give the pseudo code of how to handle the string input of the above idea??

Thank you.
Mar 17 '09 #1
8 5488
ricardopf
5 New Member
Actually, it seems to be an "light problem"...
All you need to do is write an "while" or a "for" loop.
The loop should go from 0 (zero) to the ALPH_PHONE_SIZE-1.
Probably you will read the phone from the user and store the information in a array char (or string).
Then, inside the loop you should access each position of this array/string and compute the information.
If you use a "while" loop you must keep a counter manually in order to access the array/string positions gradually. Otherwise, using a "for" loop you access the position with the loop control variable.
To know what character is the contend of a specific array/string position, you can implement a "switch:cas e" structure.
For example:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. // inside the loop
  3. switch (phone_array[POS]):
  4.    case 'a':
  5.    case 'b':
  6.    case 'c':
  7.       value = 2;
  8.       break;
  9.    case 'd':
  10.    case 'e:
  11.    case 'f':
  12.       value = 3;
  13.       break;
  14. ...
  15.    default:
  16.       break;
  17. ...
  18.  
Mar 17 '09 #2
newb16
687 Contributor
If maintainability is not an issue, I'd rather use lookup array and index it by input letter ( converted to upper case beforehand):
res = lookup[letter-'A'];
where array is like
int lookup[] = {2,2,2,3,3, ... };
Mar 17 '09 #3
whodgson
542 Contributor
The following comments explain the code amplified algorithm. The convert() function is certainly a bit clunky and could do with an oil change.
Expand|Select|Wrap|Line Numbers
  1. {char a[25]={0};                            
  2. char c=0;
  3. while(cin.get(c) )                        //get the alpha-numeric string
  4. { for(int i=0;i<25;i++)
  5.  a[i]=c;                                      //place each in array
  6.  if(c>=0 && c<=9)cout.put(c);       //traverse array and print integers
  7.  else if(c>='A' && c<='Z')convert(c);  //call func to convert text
  8.  else cout.put(c);}                        //print any integers which follow
  9. }
End input with Ctrl+Z.
Expand|Select|Wrap|Line Numbers
  1. char convert (char c)         //convert function - text to ints
  2. {
  3. switch (c)
  4. {case 65:cout<<"2";break; //A
  5.  case 66: cout<<"2";break; //B
  6.  case 67: cout<<"2";break; //C    
  7.  case 68: cout<<"3";break; //D
  8.  case 69: cout<<"3";break; //E       
  9.  case 70: cout<<"3";break; //F
  10. ............etc.......................... 
  11. case 87: cout<<"9";break; //W
  12.  case 88: cout<<"9";break; //X    
  13.  case 89: cout<<"9";break; //Y
  14.  case 90: cout<<"9";break; //Z       
  15.  default:cout<<" ";break; }                     
  16. }
/*
This is typical output.
1300-WIDER-32541-100^Z
1300-94337-32541-100

1800-THINNER-25416-100^Z
1800-8446637-25416-100^Z
Press any key to continue . .
*/
End program with another Ctrl+Z
Hope this helps.
Mar 22 '09 #4
JosAH
11,448 Recognized Expert MVP
@whodgson
Please don't try to outsmart anything and don't use those absolute values; your code fails horribly on non-ASCII encoding machines (EBCDIC is still around and alive and kicking). Use symbolic constants instead: 'A', 'B', 'C' etc. Your compiler knows what to do with them.

kind regards,

Jos
Mar 22 '09 #5
whodgson
542 Contributor
The convert() function in thread #4 was badly written and wrong.
It should have been expressed as follows:
char convert (char c)
{
switch (c)
{case 'A':cout<<"2";b reak;
case 'B': cout<<"2";break ;
case 'C': cout<<"2";break ;
case 'D': cout<<"3";break ;
case 'E': cout<<"3";break ;
case 'F': cout<<"3";break ;
--------------etc-------------------
case 'S': cout<<"7";break ;
case 'T': cout<<"8";break ;
case 'U': cout<<"8";break ;
case 'V': cout<<"8";break ;
case 'W': cout<<"9";break ;
case 'X': cout<<"9";break ;
case 'Y': cout<<"9";break ;
case 'Z': cout<<"9";break ;
default:cout<<" ";break; }
}
Thanks JosAH.....afrai d am a VSL
Mar 23 '09 #6
JosAH
11,448 Recognized Expert MVP
Why not use the short solution given in reply #3? Something like this:

Expand|Select|Wrap|Line Numbers
  1. char* d= "222333444 ... 77778889999";
  2. if (c >= 'A' && c <= 'Z') cout << d[c-'A'];
  3.  
kind regards,

Jos
Mar 23 '09 #7
crystal2005
44 New Member
@whodgson

Ahh... this works fine. Thanks a lot guys :)

But my program has different implementation. I used for loop to traverse all the character inside the char array and put the array into the switch case.
Mar 23 '09 #8
whodgson
542 Contributor
I couldn`t see how Newb16`s lookup worked and thought that it was a germ of an idea that had to be developed. Now that you`ve repeated it, think I can see
(vaguely) its meaning....... so will have a play with it. Thanks to you both.
Mar 25 '09 #9

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

Similar topics

1
2038
by: Michael Friendly | last post by:
I have a LaTeX document describing a long list of items that I want to translate to XML to treat these as a database. I've written a perl script to do the basic translation, and a basic DTD file, but I am stumped at translating LaTeX character encodings to something XML won't choke on. I found GNU recode to solve most of this, using cat milestone.tex | recode -d tex..xml | itemdb -s xml -o milestone.xml
13
4868
by: Ian Richardson | last post by:
At the moment I'm using a quicksort algorithm to sort a list of countries in alphabetic order. This worked wonderfully until someone came up with the Åland Islands... and this is at the end of the list. I'm not sure it's supposed to be. Now I could just alter my comparison so it ignores the top bit, but this would then put it at the top of the list, even before Albania... Alternatively, should I put Å after A?
0
1637
by: Tim::.. | last post by:
Hi... I'm having a big problem with a datagrid that obtains data from 2 different locations... Active Directory and SQL Database The data is inserted into a datatable and the sorted by a dataview... however due to alphabetic paging and the postback ("I think") the contacts from the SQL Database appear on ever page when you page through the datagrids
6
3432
by: py_genetic | last post by:
Hi, I'm looking to generate x alphabetic strings in a list size x. This is exactly the same output that the unix command "split" generates as default file name output when splitting large files. Example: produce x original, but not random strings from english alphabet, all lowercase. The length of each string and possible combinations is
3
2998
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 give me a function or point me to an article that explains how to convert these alphabetic number into the integers. Thanks very much in advance. Jerry.
0
1076
by: Martin H. | last post by:
Hi, I've got this problem with an enumeration of a property of a UserControl. The following code shows the problem: Public Enum TestEnum As Integer Arthur = 0 Zachary = -1 End Enum
5
3021
by: m22j | last post by:
Hello, Hopefully you guys can help me out. I'm in "intro to programming" right now and I am using visual basic express 2008. The assignment that I have due is called passwordVerification. I have to verfy that the password somebody picks is at least 6 characters long and contains numeric digits and alphabetic characters. I have been working on this assignment for what feels like forever now. Also the verification must be wrote as a function. ...
7
1553
by: emre esirik(hacettepe computer science and enginee | last post by:
I used a structer in this program. typedef struct _guitar { int serial; :serial of item at the guitar store int price; :price of item at the guitar store char builder; :builder of item at the guitar store char type; :type of item at the guitar store char model; :model of item at the guitar store }guitar;
0
7936
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7874
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8241
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8227
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6646
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5402
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3853
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.