Thread: Character program

  1. #1
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50

    Character program

    Hi there,
    I have to make c program that asks to enter a character and find out is it a number, letter or special character.

    Here is what i tried:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main(void)
    {
      char ch,a,z,c;
    
      printf("Enter a character\n");
      scanf("%c", &ch);
    
      if (c >= 'a' && c <= 'z'){
        printf("%c is a letter.\n", ch);
      }
      if (c >= '0' && c <= '9'){
        printf("%c is a number.\n", ch);
      }
        switch(ch)
      {
        case '"':
        case '{':
        case '(':
        case ')':
        case '&':
        case '^':
        case '%':
        case '*':
        case '#':
        case '!':
    
          printf("%c is a special character.\n", ch);
          break;
      }
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    10
    Well for one you should have break; inside of each case.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Do you have a specific question?
    And would you be willing to try some specialized functions for this purpose?
    isdigit - C++ Reference
    isalpha - C++ Reference
    ispunct - C++ Reference

  4. #4
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    No, just this example above the task. Character program-clipboard01-png

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    10
    I still dont understand what your question is?

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    10
    You need to be more specific.

  7. #7
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    These if loops doesnt work. Is there any other mistakes?

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    "if()" is a conditional statement, not a loop.

    Line 9: You read a character into a variable called 'ch'
    Line 11: You use 'c' (not 'ch') in your "if()" statement
    Line 14: You use 'c' (not 'ch') in your "if()" statement

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by hbranum View Post
    Well for one you should have break; inside of each case.
    This is not true.

    Not every case in a "switch()" statement should have a "break." It depends on how you want it to behave. The OP is exploiting the "fall-through" approach of a "switch()" statement.

    Sometimes, you want one thing to happen for several different values.

    Here's a quick example (it's poor code, but illustrates the concept):

    Code:
    char input;
    int exit;
    
    printf("Continue?  [Y/N] -> ");
    scanf("%c",&input);
    
    switch(input)
    {
        case 'Y':
        case 'y':
            exit = 0;
            break;
        case 'N':
        case 'n':
            exit = 1;
            break;
        default:
            break;
    }
    "exit" will be set to zero if the user enters 'Y' or 'y'.
    "exit" will be set to one if the user enters 'N' or 'n'.

  10. #10
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    I didn't understand whats wrong in line 9, but seems like it works anyway

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main(void)
    {
      char ch,a,z,c;
    
      printf("Enter a character\n");
      scanf("%c", &ch);
    
      if (ch >= 'a' && ch <= 'z'){
        printf("%c is a letter.\n", ch);
      }
      if (ch >= '0' && ch <= '9'){
        printf("%c is a number.\n", ch);
      }
        switch(ch)
      {
        case '"':
        case '{':
        case '(':
        case ')':
        case '&':
        case '^':
        case '%':
        case '*':
        case '#':
        case '!':
    
          printf("%c is a special character.\n", ch);
          break;
      }
    
        return 0;
    }

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Nothing was wrong with line 9 - I just wanted you to see that the variable you were reading into was not the same variable you were using for comparison.

  12. #12
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    oh yes,Thank you. So is this code ok now? There is no other option for "special characters"? I have to write one by another in every case " "?

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Solarwin View Post
    So is this code ok now?
    It appears to be okay. I'm assuming you tested it. I'm also assuming the character variables 'a', 'z', and 'c' are there because you plan on growing this program (they have no use in the program thus far).
    Some sticklers might recommend a "default" case in your "switch()" statement for consistency, even if it just executes a "break", but for purposes of this code, it's alright without it.

    Quote Originally Posted by Solarwin View Post
    There is no other option for "special characters"?
    You don't have specifications/instructions to tell you this? There are others you could add ('+' '-' '$' '.' for instance)

    Quote Originally Posted by Solarwin View Post
    I have to write one by another in every case " "?
    I don't understand this question. Can you clarify?

  14. #14
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Nope, i wasn't planning on growing this.
    I dont have any other instructions.I think i have to add all of C Character Set these special characters. I was wondering if there is any way how to add them without using "switch" and "cases". It is kind of boring to copy and paste all of these characters.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main(void)
    {
      char ch;
    
      printf("Enter a character: ");
      scanf("%c", &ch);
    
      if (ch >= 'a' && ch <= 'z'){
        printf("%c is a letter.\n", ch);
      }
      if (ch >= '0' && ch <= '9'){
        printf("%c is a number.\n", ch);
      }
        switch(ch)
      {
        case '"':
        case '{':
        case '(':
        case ')':
        case '&':
        case '^':
        case '%':
        case '*':
        case '#':
        case '!':
    
          printf("%c is a special character.\n", ch);
          break;
          default:
          break;
    
      }

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Programming is exciting, but it can be tedious at times. Sometimes, you have to accept that. You do have some options, though.

    1. Copy and paste them all. Time-consuming, but easy and guaranteed to meet the requirements.

    2. Look up the values on the ASCII chart, and do more ">= and <=" comparisons to check case values in bulk. This is somewhat more elegant than the "switch()" implementation you have, but it's not guaranteed to be universal, as character sets may vary on some machines.

    3. Use the "ispunct()" function I linked in post #3. This would be the easiest and nicest way to check, since it seems to check for all the characters you need it to for the "special" case. There's a link there to another page that has a table which lists which symbols "ispunct()" is valid for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with character counting program
    By shyam.sunder91 in forum C Programming
    Replies: 3
    Last Post: 05-08-2011, 05:20 AM
  2. Character count program
    By tiger6425 in forum C Programming
    Replies: 3
    Last Post: 09-04-2010, 01:35 PM
  3. Help with a character counting program!
    By Redpred in forum C Programming
    Replies: 9
    Last Post: 08-09-2006, 08:15 AM
  4. Character stack ADT program
    By alice in forum C Programming
    Replies: 1
    Last Post: 07-05-2004, 04:30 AM
  5. Character counting program
    By TankCDR in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2002, 10:01 PM