• C

convert char to numbers

The problem is to get an the inout and output file from the commnad line. In the input file there's phone numbers like
1 800 flowers
1 800 mattress
convert the text part to numbers and ouput to a seperate both the original and numerical phone number this is what I have so far
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char*argv[])
 int ch;
FILE *fp;
int count=0;
while ((ch=getchar(fp))!= EOF)

case 'A':
case 'B':
case 'C':
case 'H':
case 'I':
case 'P':
case 'S':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':

can anyone help me further I'm stuck Trying to relearn this to go back to school in Jan
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

There are couple of things that you should be doing.
Instead of assigning integer (2 to 9) to the variable ch, you should be assigning the ASCII value to ch like

case 'A':
case 'B':
case 'C':

The next thing is that, putchar takes only one parameter namely the char variable. So change that to

putchar (ch);

Also, you should be closing the file before the return (0)


Hope it helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kent OlsenDBACommented:

Hi Serena2345,

The first thing that you need to do is put a couple of sanity checks into the program.  If argc != 2 you probably have a parameter error and should display a message and exit.  If argc < 2 no file name was given so there's nothing to open.  And if you can't open the file you also need to let the user know.

As far as the actual problem goes, you're on the right track.  But you've made no provision for printing the original phone number and the modified number.  (You're converting the number without saving the one that you read.)  Here's the steps that will get you through this.

1)  Declare two character arrays of about 200 characters in length.  One is for the line that you're going to read in from the file, the other is for the line that you're going to build by substituting the appropriate character.
2)  Using fgets(), read a line from the file into the first array.  (When you hit EOF, exit the read loop.)
3)  Use strcpy() to copy the string to the new array.
4)  Loop through the new array and change each of the letters to the appropriate digit.  Remeber that you're replacing characters, not numbers.  You probably need to change upper case letters AND lower case letters!
5)  Print both strings.  Printing them on separate lines is probably better since the characters line up and it's easy to see what changes you made.
6)  Loop back and read another phone number from the file.

Note that copying the string before you convert the characters copies all of the punctuation, too.  This way the phone numbers that include dashes or parentheses will still have them.

Good Luck!

your case statement is only for uppercase letters, which isn't always the case.

so you should use the toupper()

#include <ctype.h>

int toupper( int c );

you coulde use it like this...

switch(toupper(ch)) {

Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

BTW there is no "Q" on the phone dials around here.....

hehe, there is on my office phone....but true, not on all phones.
It depends on the phone. See this link,

Therefore, you have to decide which keypad you will write this for. I'd be tempted to use the the ITU Standard E.161 keypad to write my app but it depends on what you are trying to do. In your case, for a learning excersize it probably doesn't make a big difference.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.