Solved

convert char to numbers

Posted on 2003-11-09
8
848 Views
Last Modified: 2010-04-15
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
Comment
Question by:Serena2345
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Accepted Solution

by:
rishisk earned 63 total points
ID: 9711884
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
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 62 total points
ID: 9711994

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
 
LVL 5

Expert Comment

by:g0rath
ID: 9715003
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
Industry Leaders: 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!

 
LVL 22

Expert Comment

by:grg99
ID: 9717460
BTW there is no "Q" on the phone dials around here.....

0
 
LVL 5

Expert Comment

by:g0rath
ID: 9717492
hehe, there is on my office phone....but true, not on all phones.
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9731629
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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question