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.
8 5488
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: -
...
-
// inside the loop
-
switch (phone_array[POS]):
-
case 'a':
-
case 'b':
-
case 'c':
-
value = 2;
-
break;
-
case 'd':
-
case 'e:
-
case 'f':
-
value = 3;
-
break;
-
...
-
default:
-
break;
-
...
-
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, ... };
The following comments explain the code amplified algorithm. The convert() function is certainly a bit clunky and could do with an oil change. - {char a[25]={0};
-
char c=0;
-
while(cin.get(c) ) //get the alpha-numeric string
-
{ for(int i=0;i<25;i++)
-
a[i]=c; //place each in array
-
if(c>=0 && c<=9)cout.put(c); //traverse array and print integers
-
else if(c>='A' && c<='Z')convert(c); //call func to convert text
-
else cout.put(c);} //print any integers which follow
-
}
End input with Ctrl+Z. - char convert (char c) //convert function - text to ints
-
{
-
switch (c)
-
{case 65:cout<<"2";break; //A
-
case 66: cout<<"2";break; //B
-
case 67: cout<<"2";break; //C
-
case 68: cout<<"3";break; //D
-
case 69: cout<<"3";break; //E
-
case 70: cout<<"3";break; //F
-
............etc..........................
-
case 87: cout<<"9";break; //W
-
case 88: cout<<"9";break; //X
-
case 89: cout<<"9";break; //Y
-
case 90: cout<<"9";break; //Z
-
default:cout<<" ";break; }
-
}
/*
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.
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
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
JosAH 11,448
Recognized Expert MVP
Why not use the short solution given in reply #3? Something like this: -
char* d= "222333444 ... 77778889999";
-
if (c >= 'A' && c <= 'Z') cout << d[c-'A'];
-
kind regards,
Jos
@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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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?
|
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
|
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
|
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.
| |
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
|
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.
...
|
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;
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |