Thread: Character program

  1. #16
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    I choosed third option. Try to enter number or letter You will see the mistake then.:/

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    main(void)
    {
      int i=0;
      int cx=0;
      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);
      }
    
      char str[]="\n";
      while (str[i])
      {
        if (ispunct(str[i]))ch++;
        printf("%c is a special number.\n", ch);
        i++;
    
        return 0;
        }
        }

  2. #17
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    First of all, your "return 0" is in your while loop. Good indentation is the key to better seeing the logical flow of your program and avoiding such mistakes. Here is what your code looks like with proper indentation:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
     
    main(void)
    {
        int i=0;
        int cx=0;
        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);
        }
    
        char str[]="\n";
        while (str[i])
        {
            if (ispunct(str[i]))ch++;
            printf("%c is a special number.\n", ch);
            i++;
     
            return 0;  /* this should not be inside the loop! */
        }
    }
    Second of all, it looks like you just copied the example from that link, without any idea of what you were doing. That example deals with a string, you're just checking a single character. Don't make the program more complicated than it has to be.

    Instead of that whole "while()" loop mess, you can solve this with just two lines of code.

    Slow down, take a deep breath, and look carefully at how the function is defined.

    >> int ispunct ( int c );
    >> 'c' - Character to be checked
    >> Returns a value different from zero (i.e., true) if c is a punctuation character. Returns zero (i.e., false) otherwise.

    So you need to figure out how to test the character with the "ispunct()" function. If the "ispunct()" function indicates that the character is indeed punctuation, print the appropriate message.

  3. #18
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    main(void)
    {
      int i=0;
      int cx=0;
      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);
      }
    
      char str[]="";
    
      {
        if (ispunct ( int ch ));
        printf("%c is a special number.\n", ch);
        i++;
    
    
        }
        return 0;
        }

    Nothing works Can u write that ispunct() fuction?

  4. #19
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Oh dear. Yes I can, but I don't want to show you disrespect by taking away a good learning experience. But here's a huge clue, because I'm not going to be available for the rest of the night.

    Code:
    if (/* something */){
        printf("%c is a special character.\n", ch);
      }
    You just have to figure out the "something" part.

    And FYI, there are to big problems with this line:

    Code:
    if (ispunct ( int ch ));
    1. When you pass an argument to a function, you just need to indicate the value/variable, not the data type (i.e. remove the "int").
    2. You have a semi-colon at the end of this statement. While the argument to the "if()" statement will be evaluated, even if it's true, nothing will happen. It's equivalent to this:

    Code:
    if (TRUE)
        ;  // <--- empty statement
    next line of code  // <--- will get executed because it's not part of the "if()" statement body

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There are a few excellent ways to reduce your boredom.

    First one is to include <ctype.h>, which has all kinds of functions for testing chars: isdigit(), ispunct(), isalpha(), isupper(), islower(), isctrl(), etc.

    And all the above, (and more), can be combined with && , etc.

    Now for the special chars, one way to deal with them easily, is to put them all into a single char array:
    Code:
    char special[]={"!@#$%^&*()"}; //put em all in there
    #include (string.h> in your program

    then
    Code:
    len=strlen(s);
    
    for(i=0;i<len;i++) {
       if(strchr(special, s[i])) {
          printf("Yes, it's a special char\n");
       else
          printf("No, it's not a special char\n");
    }
    Another way, mentioned by Matticus above, is to use the ASCII values. Easy to use them indirectly.

    Say I want only digits from a string:
    Code:
    int i, len;
    char s[]={"abcd12345efghijkl98pqrstuv54321wxyz");
    
    len = strlen(s);
    
    for(i=0;i<len;i++) {
       if(s[i] >= '0' &&  s[i] <= '9')
           printf("Yep! %d is a digit. \n", s[i]);
    }
    When you get into a situation where you are repeating tedious code, it's best to stop, and re-consider. Because the guys who created C were VERY bright, and they didn't want to do a bunch of tedious repetitive code, any more than you do (probably a lot less).

    Chances are, they made a function to do what you need. Not always, but very likely.
    Last edited by Adak; 12-07-2012 at 11:42 PM.

  6. #21
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    To add something, a small detail is that your program doesn't seem to handle all types of letters ......upper/lower.

  7. #22
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    I liked this way: Can somebody find my mistakes?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    main(void)
    {
      int i, len, s;
      int cx=0;
      char special[]={"!@#$%^&*()"}; //put em all in there
      char ch;
    
      printf("Enter a character: ");
      scanf("%c", &ch);
    
    
    char s[]={"abcd12345efghijkl98pqrstuv54321wxyz()"};
    
    len = strlen(s);
    
    for(i=0;i<len;i++) {
       if(s[i] >= '0' &&  s[i] <= '9')
           printf("Yep! %d is a digit. \n", s[i]);
    }
    
        return 0;
        }

  8. #23
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Well s can be one thing, not two. You have declare it as an int AND as an array of chars. This can not be done.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

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