Thread: String parsing

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    87

    String parsing

    Hi I've a string input and I have to parse it in such a way that that there can be only white space till a digit is reached and once a digit is reached, there can be only digits or white space till the string ends. Am I doing this correctly ? Thsi is just a begining :

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
            char s[50];
            int i = 0;
    
            gets(s);
    
            while (isspace(s[i]))
                    i++;
            while (isdigit(s[i]))
                    i++;
            while (isspace(s[i]))
                    i++;                
            if (s[i] != '\0')
                    printf("\nstring can't be converted\n");
            
            return (0);
    }
    I want to actually convert a string to unsigned long. So this kind of algorithm should be carried out prior to strtoul function to ensure that some of the weakness from which the strtoul function suffers like convertin 123aaaaa to 123 for eg or -123 to some unsigned value is removed. This will also ensure that when you have a string like :

    1234 78

    1234 is not returned but an error message will be printed. Because a string should only contain 1 number
    I'm also working on a function for doubles i.e. representing them in exponential format.
    Last edited by broli86; 07-03-2008 at 12:54 PM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I'm not very experienced with C string conversion functions, but from the references it seems that strtoul can already tell you that:
    Code:
        char* end;
        num = strtoul(str, &end, 10);
        //if *end is not 0 then there are unconsumed characters in str
        if (*end != '\0')
    Another thing is detecting for under- and over-flow. Overflow seems possible with strtoul, but apparently underflow needs to be detected manually.

    So altogether the conversion function might look like this:
    Code:
    int smart_to_ulong(const char* str, unsigned long* num)
    {
        char* end = str;
    
        //strtoul apparently doesn't report errors on negative values
        if (!isdigit(str[0])) {
            return 0;
        }
        errno = 0;
        *num = strtoul(str, &end, 10);
    
        //not read to the end
        if (*end != '\0') {
            return 0;
        }
    
        //over-flow
        if (errno == ERANGE) {
            return 0;
        }
        return 1;
    }
    Last edited by anon; 07-03-2008 at 01:28 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Quote Originally Posted by anon View Post
    I'm not very experienced with C string conversion functions, but from the references it seems that strtoul can already tell you that:
    Code:
        char* end;
        num = strtoul(str, &end, 10);
        //if *end is not 0 then there are unconsumed characters in str
        if (*end != '\0')
    Another thing is detecting for under- and over-flow. Overflow seems possible with strtoul, but apparently underflow needs to be detected manually.

    So altogether the conversion function might look like this:
    Code:
    int smart_to_ulong(const char* str, unsigned long* num)
    {
        char* end = str;
    
        //strtoul apparently doesn't report errors on negative values
        if (!isdigit(str[0])) {
            return 0;
        }
        errno = 0;
        *num = strtoul(str, &end, 10);
    
        //not read to the end
        if (*end != '\0') {
            return 0;
        }
    
        //over-flow
        if (errno == ERANGE) {
            return 0;
        }
        return 1;
    }
    One part in this code that's not clear to me is that you did not check for initial white space. It is possible that there may be some white space before the actual data begins. The rest I think you are right. Your logic can probably be applied to doubles as well.
    Last edited by broli86; 07-03-2008 at 04:07 PM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Ok, I added some of your logic to allow leading and trailing spaces and fixed a bug: unary + should be allowed before the number.

    Code:
    int smart_to_ulong(const char* str, unsigned long* num)
    {
        char* end = str;
    
        //advance pointer past leading spaces
        while (isspace(*str))
            ++str;
        if (!*str) //must not end of string
            return 0;
    
        //strtoul apparently doesn't report errors on negative values
        if (*str == '-') {
            return 0;
        }
        errno = 0;
        *num = strtoul(str, &end, 10);
    
        //trailing spaces allowed
        while (isspace(*end))
            ++end;
        if (*end) { //must be end of string
            return 0;
        }
    
        //over-flow
        if (errno == ERANGE) {
            return 0;
        }
        return 1;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM