Solved

convert char to numbers

Posted on 2003-11-09
8
801 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
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:Kdo
Kdo 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now