13 Replies - 699 Views - Last Post: 05 April 2011 - 08:56 AM Rate Topic: -----

#1 NoRemorse  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 11

Convert characters to numbers in C

Posted 26 March 2011 - 03:51 AM

#include  <stdio.h>


int main(void)
{
         
   char nbr[10];
   int i;
   printf("Enter phone number:");
   scanf("%s",&nbr);
   for(i=0;i<1;i++){
                    if(nbr[i] == 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 ){
                              printf("%c",nbr[i]);
                              
                              }
                              else
                              printf("bla");
                              
                    }          
                          
system("Pause");      
}


So I am supposed to create a program that asks a phone number and translates all the letters to numbers, but I dont know how to do it. I tried to print out numbers that already are in the phone number, but it also prints chars. Help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Convert characters to numbers in C

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Convert characters to numbers in C

Posted 26 March 2011 - 04:22 AM

if(nbr[i] == 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 )

For one thing, you can't compare multiple values like that. And your chars need to have single quotes around them, like '0'...those ARE char, not actual numbers, because they're part of a string. Why not shorten that up by testing if the char is greater than or equal to the lowest and less than or equal to the highest?
Was This Post Helpful? 1
  • +
  • -

#3 FamiHug  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 34
  • Joined: 03-December 10

Re: Convert characters to numbers in C

Posted 26 March 2011 - 05:10 AM

you can use isdigit(int c) defined in header file ctype.h to check the character is digit (0-9)
and if true, prints out nbr[i] - 48 (because your input gets the char, 48 is the '0' character in ascii table)
printf("%d",nbr[i] - 48); 

Was This Post Helpful? 0
  • +
  • -

#4 NoRemorse  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 11

Re: Convert characters to numbers in C

Posted 26 March 2011 - 06:03 AM

#include  <stdio.h>


int main(void)
{      
   char nbr[10];
   int i;
   printf("Enter phone number:");
   scanf("%s",&nbr);
   for(i=0;i<1;i++){
                    if(nbr[i] >= '0' || nbr[i] <= '9' ){
                              printf("%c",nbr[i]);

                              }
                              else
                               
                              switch(nbr){
                                case 'A': printf("2");
                                case 'B': printf("2"); 
                                case 'C': printf("2");
                                break;
                                case 'D': printf("3");
                                case 'E': printf("3"); 
                                case 'F': printf("3");
                                break;
                                case 'G': printf("4");
                                case 'H': printf("4"); 
                                case 'I': printf("4");
                                break;
                                case 'J': printf("5");
                                case 'K': printf("5"); 
                                case 'L': printf("5");
                                break;
                                case 'M': printf("6");
                                case 'N': printf("6"); 
                                case 'O': printf("6");
                                break;
                                case 'P': printf("7");
                                case 'R': printf("7"); 
                                case 'S': printf("7");
                                break;
                                case 'T': printf("8");
                                case 'U': printf("8"); 
                                case 'V': printf("8");
                                break;
                                case 'W': printf("9");
                                case 'X': printf("9"); 
                                case 'Y': printf("9");
                                break;
                                default: printf("Invalid input");
                                }}
                                      
                          
system("Pause");      
}   


Well how should I make the loop so that it would read the next character also not only the first and it still reads chars also not only numbers?
And what quantity I should put in the switch() then so it would compile?
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: Convert characters to numbers in C

Posted 26 March 2011 - 07:08 AM

If the value being considered is 'A', then this will print "222".
case 'A': printf("2");
case 'B': printf("2"); 
case 'C': printf("2");
break;



Two ways to attack it, break on every one:
case 'A': printf("2"); break;
case 'B': printf("2"); break;
case 'C': printf("2"); break;



Or, probably preferred, break after the list of matches, e.g.
case 'A': 
case 'B': 
case 'C': 
	printf("2"); break;
case 'D':
case 'E':
case 'F': 
	printf("3"); break;



You have a few other issues:
// you want to loop for one?  are you sure?
for(i=0;i<1;i++) {
	// let's put the current value in a variable
	char ch = nbr[i];
	// you have a big logic thing here, you want "and"
	// if(nbr[i] >= '0' || nbr[i] <= '9' ){
	if(ch>='0' && ch <='9') {
		printf("%c", ch);
	} else {
		// switch what?
		// switch(nbr) {
		// try
		switch(ch) {



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#6 NoRemorse  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 11

Re: Convert characters to numbers in C

Posted 26 March 2011 - 01:23 PM

#include  <stdio.h>


int main(void)
{      
   char nbr[7];
   int i,ch;
   printf("Enter phone number:");
   scanf("%s",&nbr);
   for(i=0;i<7;i++){
                    if(nbr[i] >= '0' && nbr[i] <= '7' ){
                              printf("%c",nbr[i]);
                              }
                              else{
                              switch(ch){
                                case 'A': 
                                case 'B':  
                                case 'C':
                                      printf("2");break;
                                case 'D': 
                                case 'E':  
                                case 'F': 
                                     printf("3");break;
                                case 'G': 
                                case 'H':  
                                case 'I': 
                                     printf("4");break;
                                case 'J': 
                                case 'K': 
                                case 'L':
                                      printf("5");break;
                                case 'M': 
                                case 'N':  
                                case 'O': 
                                     printf("6");break;
                                case 'P': 
                                case 'R':  
                                case 'S': 
                                     printf("7");break;
                                case 'T': 
                                case 'U':  
                                case 'V': 
                                     printf("8");break;
                                case 'W': 
                                case 'X':  
                                case 'Y': 
                                     printf("9");break;
                                default: printf("Invalid input");
                                }}}
                                      
                          
system("Pause");      
}     


Well numbers work, but the switch treats all chars still like default and I get : Ivalid input.
Any ideas?
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Convert characters to numbers in C

Posted 26 March 2011 - 02:02 PM

char nbr[7];
   int i,ch;
   printf("Enter phone number:");
   scanf("%s",&nbr);
   for(i=0;i<7;i++){
                    if(nbr[i] >= '0' && nbr[i] <= '7' ){
                              printf("%c",nbr[i]);
                              }
                              else{
                              switch(ch){


And where, exactly, do you set ch to something valid?
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: Convert characters to numbers in C

Posted 26 March 2011 - 05:44 PM

The ch was to make things clearer and your life easier. If you don't want to use it, that's fine.

While some C compilers are picky about how you declare variables, you should still try to declare and initialize at the same time whenever possible. Every version of C will let you declare at the top of a block. It's good to take advantage of this. The less scope a variable has, the less chance of it getting redefined, lost, or forgotten. Here, you've failed to initialize.
Was This Post Helpful? 0
  • +
  • -

#9 NoRemorse  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 11

Re: Convert characters to numbers in C

Posted 27 March 2011 - 02:00 AM

Declare it how and at the top of what block? Switch?
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: Convert characters to numbers in C

Posted 27 March 2011 - 03:09 AM

void foo() { /* block begins */
   int i; /* declare */
   i = 0; /* define */
   for(;i<42; i++) { /* block begins */
      int j = i * i; /* declare and define */
      /* stuff */
   } /* block ends */
  /* variable j no longer exists */




int i;
for(i=0;i<7;i++){
   char ch = nbr[i]; /* declare and define */
   /* from this point forward, we can use ch instead of nbr[i] */
   if(ch>='0' && ch<= '7' ) {
      printf("%c",ch);
   } else{
      switch(ch){



You can just as easily do switch(nbr[i]). The new variable offers the advantage of being shorter and specific.

It could also be used as holder for conversion:
char ch = nbr[i]; /* declare and define */
if (ch>='A' && ch<='C') { ch='2';
} else if (ch>='D' && ch<='F') { ch='3';
/* ... */
} else if (ch<'0' || ch>'9') { ch='*';
}
printf("%c",ch);


Was This Post Helpful? 2
  • +
  • -

#11 NoRemorse  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 11

Re: Convert characters to numbers in C

Posted 27 March 2011 - 07:40 AM

for(i=0;i<7;i++)

Thank you very much baavgai, can you also tell me how I could make that for loop read only as many chars as there are entered not 7 all the time?
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Convert characters to numbers in C

Posted 27 March 2011 - 08:19 AM

Google strlen.
Was This Post Helpful? 1
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: Convert characters to numbers in C

Posted 27 March 2011 - 08:23 AM

Strlen is the standard function for checking string length. However, since all C strings are null terminated, you can loop until you hit a zero.

e.g.
for(i=0;s[i]!=0;i++)


Was This Post Helpful? 1
  • +
  • -

#14 NoRemorse  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 11

Re: Convert characters to numbers in C

Posted 05 April 2011 - 08:56 AM

Thank you guys for the big help:)

This post has been edited by NoRemorse: 05 April 2011 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1