#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Testing characters


    I'm trying to understand how to write a program where I have to have a function testing the characters of input. I understand some basics, such as a validity check ! or EOF, and detecting digits. However, it gets complicated when I want to check for digits and a decimal, and making sure only a set amount of digits are past the decimal.

    For starters, I'm a bit of a doorknob with understanding C. It took me almost a month to understand the simple concept of functions and how to write multiple functions. Developing a function to test for acceptable characters is proving to be difficult for me, and I'd like to ask for help.

    I understand a program can't be written just to detect digits and reject unacceptable characters. Conversion is needed, but here's where I'm getting stuck. Say if I wanted to input 51.50 versus 51.505-- what are the differences in the character conversions? How are the steps going to differ? Is using a loop for each element appropriate for C? Thanks!
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,396
    Rep Power
    1871
    Use strtod.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    int main()
    {
        char input[] = "1234.5678\n";
        char *end;
        double result = strtod(input,&end);
        printf("Status of conversion=%d\n", errno);
        printf("Result=%f\n", result);
        printf("String length of number=%d\n", (int)(end-input));
        printf("Following character=%d\n", *end);
        
        return 0;
    }
    
    $ gcc foo.c
    $ ./a.out 
    Status of conversion=0
    Result=1234.567800
    String length of number=9
    Following character=10
    You should be able to pretty much nail down anything you want to know about your decimal input.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Is this more efficient than using isdigit as a function?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,396
    Rep Power
    1871
    > Is this more efficient than using isdigit as a function?
    Given the limiting speed of any stream of I/O to be able to deliver characters to your program, probably not.

    Also, how long is it going to take you to develop the code using isdigit?

    You might shave a few microseconds per validation, but if it takes you 3 days to write and debug your new approach, is it that much of a bargain?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by salem
    > Is this more efficient than using isdigit as a function?
    Given the limiting speed of any stream of I/O to be able to deliver characters to your program, probably not.

    Also, how long is it going to take you to develop the code using isdigit?

    You might shave a few microseconds per validation, but if it takes you 3 days to write and debug your new approach, is it that much of a bargain?
    The reason I ask is because my C programming teacher is set on the idea that certain functions can make a code look "cleaner" and be more efficient. I know developing the code would take me longer, if not just as long. I did say I'm a bit of a doorknob...
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    I just want to make sure I understand something-- if I enter the number "48," it converts to 52 and 56 in ASCII, correct?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Originally Posted by redsumac
    I just want to make sure I understand something-- if I enter the number "48," it converts to 52 and 56 in ASCII, correct?
    Yes that's correct for ascii based systems. If you want a portable program then you should check out the functionality in ctype.h...

    Code:
    int isalnum(int c);
    int isalpha(int c);
    int isascii(int c);
    int isblank(int c);
    int iscntrl(int c);
    int isdigit(int c);
    int isgraph(int c);
    int islower(int c);
    int isprint(int c);
    int ispunct(int c);
    int isspace(int c);
    int isupper(int c);
    int isxdigit(int c);
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    I was wondering about that. Could I use errno.h and ctype.h?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Originally Posted by redsumac
    I was wondering about that. Could I use errno.h and ctype.h?
    Well you can use them together in the same program but the functionality in ctype.h doesn't set the errno.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    I could use isdigit with ctype.h, because I do want portability.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Originally Posted by redsumac
    Say if I wanted to input 51.50 versus 51.505-- what are the differences in the character conversions? How are the steps going to differ? Is using a loop for each element appropriate for C? Thanks!
    Then strtod is your friend.
    Originally Posted by redsumac
    I could use isdigit with ctype.h, because I do want portability.
    That's fine if you want to reimplement the functionality of something like strtod. Just keep in mind that it's going to be a lot more complicated than the version using strtod.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by Lux Perpetua
    Then strtod is your friend. That's fine if you want to reimplement the functionality of something like strtod. Just keep in mind that it's going to be a lot more complicated than the version using strtod.
    Could you elaborate? I know some of the programs I've seen include a couple of functions which is complicated enough, so the cleaner I could get it, the better I suppose.
  24. #13
  25. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,396
    Rep Power
    1871
    > Could you elaborate? I know some of the programs I've seen include a couple of functions which is complicated enough,
    > so the cleaner I could get it, the better I suppose.
    How much simpler that post #2 do you want it to be?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo