C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-03-2008, 12:50 PM   #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.
broli86 is offline   Reply With Quote
Old 07-03-2008, 01:23 PM   #2
The larch
 
Join Date: May 2006
Posts: 3,222
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;
}
__________________
I might be wrong.

Quote:
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).

Last edited by anon; 07-03-2008 at 01:28 PM.
anon is offline   Reply With Quote
Old 07-03-2008, 02:27 PM   #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.
broli86 is offline   Reply With Quote
Old 07-03-2008, 05:06 PM   #4
The larch
 
Join Date: May 2006
Posts: 3,222
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.

Quote:
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).
anon is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
char Handling, probably typical newbie stuff Neolyth C Programming 16 06-21-2009 04:05 AM
Message class ** Need help befor 12am tonight** TransformedBG C++ Programming 1 11-29-2006 11:03 PM
String editor for a sentence inputted by a user - any suggestions or ideas? the_newbug C Programming 4 03-03-2006 02:11 AM
creating class, and linking files JCK C++ Programming 12 12-08-2002 02:45 PM
Warnings, warnings, warnings? spentdome C Programming 25 05-27-2002 06:49 PM


All times are GMT -6. The time now is 07:23 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22