• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 860
  • Last Modified:

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
etc
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;
fp=fopen(argv[1],"r");
while ((ch=getchar(fp))!= EOF)

switch(ch){
case 'A':
case 'B':
case 'C':
      ch=getchar(2);
      break;
case'D':
case'E':
case'F':
      ch=3;
      break;
case'G':
case 'H':
case 'I':
      ch=4;
      break;
case'J':
case'L':
case'K':
      ch=5;
      break;
case'M':
case'N':
case'O':
      ch=6;
      break;
case 'P':
case'Q':
case'R':
case 'S':
      ch=7;
      break;
case'T':
case 'U':
case 'V':
      ch=8;
      break;
case 'W':
case 'X':
case 'Y':
case 'Z':
      ch=9;
      break;



putchar(ch,stdout);
count++;
}
return(0);
}
can anyone help me further I'm stuck Trying to relearn this to go back to school in Jan
0
Serena2345
Asked:
Serena2345
2 Solutions
 
rishiskCommented:
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':
     ch='2';
     break;

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)

fclose(fp);

Hope it helps.
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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!
Kent

0
 
g0rathCommented:
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)) {
...
}

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

0
 
g0rathCommented:
hehe, there is on my office phone....but true, not on all phones.
0
 
ExceterCommented:
It depends on the phone. See this link,
http://www.dialabc.com/motion/keypads.html

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.

Cheers!
Exceter
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now