Thread: If statment qustion

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    6

    If statment qustion

    I need to create a program so when the person input a character and if its between 0 to 9 it prints outs and says its a digit if not it should say it a character ... i just dont know the if part would work, maybe:

    Code:
    if (a between '0' to '9') {printf ("%c its a digit", a);}

  2. #2
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Code:
    c=getchar();
    if(c>='0' && c<='9') printf("%c is a digit", c);
    else printf("Its a character");
    If i remember correctly, there's a function isdigit aswell.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    yeah that's seem to work, thanks for the help, also i like to know when a character is taken from the user how can it be transferred to an upper case and then outputted?

  4. #4
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    If you mean making lower cases a-z into A-Z, then:

    Code:
    int c=getchar();
    if(c>='a' && c<='z') 
    {
       c=c - 32; 
       printf("%c", c);
    }
    Why 32? Because of ASCII table.
    Character a has value of 97, A has a value of 65.
    97-65 = 32

    And all of the characters go after each other, increasing their value by 1. Table:
    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion
    Last edited by Tool; 11-20-2009 at 08:25 PM.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Tool View Post
    If you mean making lower cases a-z into A-Z, then:

    Code:
    int c=getchar();
    if(c>='a' && c<='z') 
    {
       c=c - 32; 
       printf("%c", c);
    }
    Why 32? Because of ASCII table.
    Character a has value of 97, A has a value of 65.
    97-65 = 32

    And all of the characters go after each other, increasing their value by 1. Table:
    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion
    Sure, if you want your program to not be very portable...
    Try that on a mainframe and see how well it works.

    There are functions called tolower() & toupper() that do the conversion for you in a portable way. Use them.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    Yeah thats exactly what i wanted, you just made it clear, thanks so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Qustion problem
    By Gardul in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2005, 01:06 AM
  2. quick qustion
    By Oluf in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2004, 07:30 AM
  3. some qustion
    By NEO_4583 in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2003, 08:11 PM
  4. If statment +text
    By Zewu in forum C++ Programming
    Replies: 4
    Last Post: 01-07-2002, 04:31 PM