Thread: Determining length needed for string

  1. #1
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294

    Determining length needed for string

    Ok, my problem is that my expand function prints the expanded version of the shorthand to stdout, and I would like it to return a pointer to a newly created string containing the expanded version. I have no idea how many notations are going to be provided at runtime, so my question is as follows: how can I determine how much space is exactly needed for the newly expanded string so I can dynamically allocate the space. Ahhh I hope a seasoned vet can help me out, or point out why this is a proposterous idea.

    Code:
    Code:
    #include <stdio.h>
    #include <string.h>
    void expand( char * , int );
    void print_range( char , char );
    int main( int argc , char **argv )
    {
        if( argc != 2 )
            printf( "Usage: %s [shorthand notation]\n" , argv[ 0 ] );
        else{
            expand( argv[ 1 ] , strlen( argv[ 1 ] ));
            printf( "\n" );
    
        }
        return 0;
    }
    void expand( char * string , int str_len ){
        int i = 0 , partner = 0 , hyphen_pair = 0;
        char * start = NULL , * end = NULL;
        static char upper[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        static char lower[27] = "abcdefghijklmnopqrstuvwxyz";
        static char digits[11] = "0123456789";
        
        while( i < str_len ){
            if( string[ i ] == '-' && ( i == 0 || i == ( str_len - 1 ) ) )
                printf( "-" );    // leading or trailing '-'
            else    /* parsing for "upper ranges". either right next to each other or '-' seperated is allowable */
                if( ( start = strchr( upper , string[ i ] ) ) != NULL ){
                    end = NULL;
                    if( ( end = strchr( upper , string[ i + 1 ] ) ) != NULL ){
                        print_range( * start, * end );/* next char is also upper, allowable syntax by my "standards" */
                        partner++;
                    }
                    else if( ( end = strchr( lower , string[ i + 1 ] ) ) != NULL ){ // 2nd operand is lower-case
                        printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
                        partner++;
                    }
                    else if( ( end = strchr( digits , string[ i + 1 ] ) ) != NULL ){ // 2nd operand is digit
                        printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
                        partner++;
                    }
                    else 
                        if( string[ i + 1 ] == '-' ){    // "upper-" found, checking data type of 2nd operand
                            hyphen_pair++;
                            if( ( end = strchr( upper , string[ i + 2 ] ) ) != NULL ){    // "upper-upper" found
                                print_range( * start , * end );
                            }
                            else if( ( end = strchr( lower , string[ i + 2 ] ) ) != NULL )    // "upper-lower" found; error
                                printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
                            else if( ( end = strchr( digits , string[ i + 2 ] ) ) != NULL  )    // "upper-digit" found; error
                                printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
                        }
                        else{
                            printf( "\n[!] invalid character detected, '%c': ABORT!\n" , string[ i + 1 ] );
                            break;    // error recovery is non-existent if invalid character is used (non alpha_numeric or not a hyphen(-)
                        }
                    start = NULL;
                }else if( ( start = strchr( lower , string[ i ] ) ) != NULL ){
                /* parsing for "lower ranges". either right next to each other or '-' seperated is allowable */
                    end = NULL;
                    if( ( end = strchr( lower , string[ i + 1 ] ) ) != NULL ){ 
                        print_range( * start, * end );/* next char is also lower, allowable syntax by my "standards" */
                        partner++;
                    }
                    else if( ( end = strchr( upper , string[ i + 1 ] ) ) != NULL ){ // 2nd operand is upper-case; error
                        printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
                        partner++;
                    }
                    else if( ( end = strchr( digits , string[ i + 1 ] ) ) != NULL ){ // 2nd operand is digit; error
                        printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
                        partner++;
                    }
                    else 
                        if( string[ i + 1 ] == '-' ){    // "lower-" found, checking data type of 2nd operand
                            hyphen_pair++;
                            if( ( end = strchr( lower , string[ i + 2 ] ) ) != NULL ){    // "lower-lower" found
                                print_range( * start , * end );
                            }
                            else if( ( end = strchr( upper , string[ i + 2 ] ) ) != NULL )    // "lower-upper" found; error
                                printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
                            else if( ( end = strchr( digits , string[ i + 2 ] ) ) != NULL  )    // "lower-digit" found; error
                                printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
                        }
                        else{
                            printf( "\n[!] invalid character detected, '%c': ABORT!\n" , string[ i + 1 ] );
                            break;
                        }
                    start = NULL;
            }
            else    /* parsing for "digit ranges". either right next to each other or '-' seperated is allowable */
                if( ( start = strchr( digits , string[ i ] ) ) != NULL ){
                    end = NULL;
                    if( ( end = strchr( digits , string[ i + 1 ] ) ) != NULL ){
                        print_range( * start, * end );/* next char is also digit, allowable syntax by my "standards" */
                        partner++;
                    }
                    else if( ( end = strchr( upper , string[ i + 1 ] ) ) != NULL ){ // 2nd operand is upper-case; error
                        printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
                        partner++;
                    }
                    else if( ( end = strchr( lower , string[ i + 1 ] ) ) != NULL ){ // 2nd operand is lower; error
                        printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
                        partner++;
                    }
                    else 
                        if( string[ i + 1 ] == '-' ){    // "digit-" found, checking data type of 2nd operand
                            hyphen_pair++;
                            if( ( end = strchr( digits , string[ i + 2 ] ) ) != NULL ){    // "digit-digit" found
                                print_range( * start , * end );
                            }
                            else if( ( end = strchr( upper , string[ i + 2 ] ) ) != NULL )    // "digit-upper" found; error
                                printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
                            else if( ( end = strchr( lower , string[ i + 2 ] ) ) != NULL  )    // "digit-lower" found; error
                                printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
                        }
                        else{
                            printf( "[!] invalid character detected, '%c': ABORT!\n" , string[ i + 1 ] );
                            break;
                        }
                    start = NULL;
            }
            else{
                printf( "[!] invalid character detected, '%c': ABORT!\n" , string[ i ] );
                break;
            }
            ////////////////////////////
            if( partner ){//////////////
                i += 2;/////////////////
                partner = 0;////////////  this block sets the next starting position in string[]
            }///////////////////////////
            else if( hyphen_pair ){/////    
                i += 3;/////////////////
                hyphen_pair = 0;////////
            }///////////////////////////
            else////////////////////////
                i++;////////////////////
        }    ////////////////////////////
        return;
    }
    void print_range( char start , char end ){
        int i;
        if( start < end ) /* fowards */
            for( i = ( int ) start; i <= ( int ) end; ++i )
                printf( "%c" , i );
        else if( start > end )    /* backwards */
            for( i = ( int ) start; i >= ( int ) end; --i )
                printf( "%c" , i );
        else
            printf( "%c" , start );
        return;
    }
    Thanks in advance for any and all help!

    PS I know its quite a few lines of code. Basic pseudocode is:
    Code:
    expand()
    
    while( all characters haven't been read ){
            if( char is '-' and is either leading or trailing )
                    print '-'
            if ( a valid range is found )
                    print expanded notation of range
            else
                   if( an invalid range or an invalid char is found )
                            print error message
                            if( invalid char )
                                 break while
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Can you explain in English what your program is doing? Give an example of input and expected output.

    You seem to be using strchr (and your arrays) simply to tell whether a char is upper or lowercase or a digit, but there are specific functions for that:
    Code:
    #include <ctype.h>
    isupper(c);
    islower(c);
    isdigit(c);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You are printing up the expanded string, char by char - so there's your counter:

    1) You might be able to use i

    but if not,

    2) Use a separate count variable of type int, which you will initialize to 0.

    Code:
    void print_range( char start , char end ){
        int i;
        if( start < end ) /* fowards */
            for( i = ( int ) start; i <= ( int ) end; ++i )
                printf( "%c" , i );
        else if( start > end )    /* backwards */
            for( i = ( int ) start; i >= ( int ) end; --i )
                printf( "%c" , i );
        else
            printf( "%c" , start );
        return;
    }
    I don't understand the "i = (int)", part of the code, or i <= (int) end.

    The variable i is already declared as an int, just above the for loop, and there is no need to make end into an int that I can see, so far.

    Can you make a simple example of what you want the program to do, rather than describe it? When I read your description, I have more questions than I had before I read it.

    Please, an example!

  4. #4
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    lol seems like im causing more questions than providing them

    usage:

    gcc main.c -o expand

    ./expand [shorthand notation] , where shorthand notation would be a range of two chars seperated by a hyphen. ie 0-9 would expand to 0123456789 and a-d5-2 would be abcd5432

    leading and trailing hyphens should be taken literally ie -a-d would be -abcd and 8-1- would be 87654321-

    my code before was a bit "flawed" and im not quite sure how to edit my posts? Can anyone tell me how?

    new code:
    Code:
    /* Write a function expand(s1,s2) that expands shorthand notations like
       a-z in the string s1 into the equivalent complete list abc...xyz in s2.
       Allow for letters of either case and digits, and be prepared to handle
       cases like a-b-c and a-z0-9 and -a-z. Arrange that a leading or
       trailing - is taken literally. */
    #include <stdio.h>
    #include <string.h>
    void expand( char * , int );
    void print_range( char , char );
    int is_valid_char( char );
    int main( int argc , char **argv )
    {
        if( argc != 2 )
            printf( "Usage: %s [shorthand notation]\n" , argv[ 0 ] );
        else{
            expand( argv[ 1 ] , strlen( argv[ 1 ] ));
            printf( "\n" );
            getchar();
        }
        return 0;
    }
    void expand( char * string , int str_len ){
        int i = 0 , hyphen_done = 0 , hyphen_cont = 0;
        char * start = NULL , * end = NULL;
        static char upper[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        static char lower[27] = "abcdefghijklmnopqrstuvwxyz";
        static char digits[11] = "0123456789";
        
        while( i < str_len ){
            if( string[ i ] == '-' && ( i == 0 || i == ( str_len - 1 ) ) )
                printf( "-" );    // leading or trailing '-'
            else    /* parsing for "upper ranges" */
                if( ( start = strchr( upper , string[ i ] ) ) != NULL ){
                    end = NULL;
                    if( string[ i + 1 ] == '-' ){    // "upper-" found, checking data type of 2nd operand
                        if( ( end = strchr( upper , string[ i + 2 ] ) ) != NULL ){    // "upper-upper" found
                            print_range( * start , * end );
                            if( string[ i + 3 ] == '-' )
                                hyphen_cont++;
                            else
                                hyphen_done++;
                        }
                        else{
                            if( is_valid_char( string[ i + 2 ] ) )
                                printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
                            else{
                                printf( "\n[!] invalid syntax, try ./expand --usage for examples: ABORT!\n" );
                                break;
                            }
                        }
                    }
                    else{
                        printf( "\n[!] invalid syntax, try ./expand --usage for examples: ABORT!\n" );
                    }
                    start = NULL;
                }else /* parsing for "lower ranges" */
                    if( ( start = strchr( lower , string[ i ] ) ) != NULL ){
                        end = NULL;
                        if( string[ i + 1 ] == '-' ){    // "lower-" found, checking data type of 2nd operand
                            if( ( end = strchr( lower , string[ i + 2 ] ) ) != NULL ){    // "lower-lower" found
                                print_range( * start , * end );
                                if( string[ i + 3 ] == '-' )
                                    hyphen_cont++;
                                else
                                    hyphen_done++;
                            }
                            else{
                                if( is_valid_char( string[ i + 2 ] ) )
                                    printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
                                else{
                                    printf( "\n[!] invalid syntax, try ./expand --usage for examples: ABORT!\n" );
                                    break;
                                }
                            }
                        }
                        else{
                            printf( "\n[!] invalid syntax, try ./expand --usage for examples: ABORT!\n" );
                        }
                        start = NULL;
                    }
                    else    /* parsing for "digit ranges" */
                        if( ( start = strchr( digits , string[ i ] ) ) != NULL ){
                        end = NULL;
                        if( string[ i + 1 ] == '-' ){    // "digit-" found, checking data type of 2nd operand
                            if( ( end = strchr( digits , string[ i + 2 ] ) ) != NULL ){    // "digit-digit" found
                                print_range( * start , * end );
                                if( string[ i + 3 ] == '-' )
                                    hyphen_cont++;
                                else
                                    hyphen_done++;
                            }
                            else{
                                if( is_valid_char( string[ i + 2 ] ) )
                                    printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
                                else{
                                    printf( "\n[!] invalid syntax, try ./expand --usage for examples: ABORT!\n" );
                                    break;
                                }
                            }
                        }
                        else{
                            printf( "\n[!] invalid syntax, try ./expand --usage for examples: ABORT!\n" );
                            break;
                        }
                        start = NULL;
                    }
                    else
                        if( string[ i ] != '-' ){
                            printf( "\n[!] invalid syntax, try ./expand --usage for examples: ABORT!\n" );
                            break;
                        }
                            
            if( hyphen_cont ){
                i += 2;
                hyphen_cont = 0;    //this is where how many positions in string to jump is determined
            }
            else
                if( hyphen_done ){
                    i += 3;
                    hyphen_done = 0;
                }
            else
                i++;
        }
        return;
    }
    void print_range( char start , char end ){
        if( start < end ) /* fowards */
            for( ; start <= end; ++start )
                printf( "%c" , start );
        else if( start > end )    /* backwards */
            for( ; start >= end; --start )
                printf( "%c" , start );
        else
            printf( "%c" , start );
        return;
    }
    int is_valid_char( char c ){
        return ( ( c >= '0' && c <= '9' ) || ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) ? 1 : 0;
    }
    Last edited by jwroblewski44; 07-18-2012 at 08:15 PM.

  5. #5
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    also as many ranges may be concatanated onto each other at runtime ( i had no need for a limit thus far )

  6. #6
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    does a linked list of char's sound like it could do what I want?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jwroblewski44
    does a linked list of char's sound like it could do what I want?
    That will just make life more difficult for you. Given the range input, you can determine the length of the range, thus allowing you to dynamically create an array of chars that will store all the characters of the range.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jwroblewski44 View Post
    does a linked list of char's sound like it could do what I want?
    Arrays are much easier to work with, and debug. Linked lists are OK if you really need to conserve memory AND your run-time is not hurt by including them.

    So in your code, you might:

    1) check the first char, if it's a hyphen put it into the expanded array, exp[]. If it's not a hyphen, begin putting it and all the letters greater than it, until you reach the last letter. Include any hyphens you find.

    2) If there is a hyphen as the last char in the given string, append it onto the exp[] string.

    If you want to keep each work separate at first, I'd use a 2D array (with rows and columns), and have one word per row.

    word1
    word2
    word3
    ...

    Otherwise, you can use one single array to hold everything:
    word1word2word3...

    Whichever way you do it, remember that if you want to use any string functions (seems like you might want to use strcat()), you need to add the '\0' (end of string marker char), to the end of each string, BEFORE you try to treat the char's as a string.

    A collection of letters or char's, are just a bunch of char's, until they have that end of string char on the end - then and only then, are they a C string.

  9. #9
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    thank you for the super fast replies!

    im trying to determine the space needed for the char array with something like this:
    Code:
    char * ret_range_ptr( char start , char end ){
        char * expanded = malloc( ( sizeof( char ) * (  ( start < end ) ? ( end - start ) : ( start - end )  ) ) + 1 );
        if( start < end ) /* fowards */
            for( ; start <= end; ++start )
                sprintf( expanded , "%c" , start );
        else if( start > end )    /* backwards */
            for( ; start >= end; --start )
                sprintf( expanded , "%c" , start );
        else
            sprintf( expanded , "%c" , start );
        return expanded;
    }
    it's returning the last char instead of the whole string

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I sort of agree with Adak. I would actually do things a little differently though.

    I would make a helper function that can just handle expansion of phrases like "1-9" and "A-Z". Make a function that can expand these and store them into an array argument. Make this easier-to-do function first and make sure that it works well.

    Then, in your regular "expand()" function all you need to do is look for those phrases in the shorthand string. If you can do that, you can pass in the phrase to the helper function and some array storage and it will fill it in for you. For things that are definitely not phrases, expand() can just copy things character by character or error out.

    For example: If a specific character is a digit or a letter, you will want to check the next and next-next characters to find those phrases. So if you find '0', '-' and '9', then you just copy those into a handy argument string, and pass that and some storage to the helper function... Suppose that it's not that nice, then you will want to make error notifications. Keep going until you run out of stuff to do! Eventually you'll pass back a nice expanded string.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can use each char as a number - since it IS a number inside the computer. A table of those numbers to char's, can be downloaded easily. Google ASCII table, and keep it handy. It's a must for programming.

    Forward series length will be the last char (in the series), ascii number value, minus first char (in the series), ascii number value, plus the number of hyphens in the string on the front and the back of the series, plus one. (plus two if you want to add the end of string char to it).

    That is for any one series, where the series uses ONLY letters OR numbers, BUT NOT both in the same series.

    For example a-c has a length of three, since 'c' - 'a' is two, and we add +1 to it (or plus two if you want it to be a string). The series -a-d- has length 6, since 'd' - 'a' is 3, and then we add +1 to it, and then we add +2 because it has both before and after hyphen char's on it.

    Code:
    printf("A - F is %d char's long\n",('F' - 'A')+1);
    You want the char's to stay char's for this. You don't need to cast the char's as int's. The char's are already represented as numbers, inside the computer, - that's the ascii character set, (which all computers won't use the exact same character set, btw.)

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by jwroblewski44 View Post
    thank you for the super fast replies!

    im trying to determine the space needed for the char array with something like this:
    Code:
    char * ret_range_ptr( char start , char end ){
        char * expanded = malloc( ( sizeof( char ) * (  ( start < end ) ? ( end - start ) : ( start - end )  ) ) + 1 );
        if( start < end ) /* fowards */
            for( ; start <= end; ++start )
                sprintf( expanded , "%c" , start );
        else if( start > end )    /* backwards */
            for( ; start >= end; --start )
                sprintf( expanded , "%c" , start );
        else
            sprintf( expanded , "%c" , start );
        return expanded;
    }
    it's returning the last char instead of the whole string
    It's not working right because the way that you are calling sprintf() overwrites the same character over and over. The 'expanded' variable represents the starting point. To fix it I would keep track of where you are supposed to be in expanded with a variable, called i perhaps, and change sprintf() like so.
    Code:
    sprintf(expanded + i, "%c", start);
    Every time you copy a character, i will increase by 1. This is one way you can write the helper function I alluded to. You now just need to write the code to use it.

    You can always add the starting hyphen to what will be the final string before you do this part of the algorithm, and add a possible hyphen to the final string if it is supposed to be there at the very end. Unless you are really conservative with memory, you won't even need to reallocate to accommodate the hyphens. And make sure you free the sections after you copy them over to the final string.
    Last edited by whiteflags; 07-19-2012 at 03:26 AM.

  13. #13
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    ahaaahaa thank you very much whiteflags, i always forget about the offset for a pointer. You rock, and so does Adak, laserlight and oogabooga.

  14. #14
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    but as far as my malloc call, that looks correct?

  15. #15
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Ok so my code functions correctly as is. But at line 35, 57, and 80, I print out the expanded range, when what I really want to do is concatenate it onto a base string, and return that base string to main. My problem is that I'm not sure how to declare the base string, as to not provide for any situation of out of bounds exceptions. Because any amount of ranges is currently acceptable ( and I dont really want to change that ), I'm not sure how to determine the space needed. Any suggestions??

    Code:
    /* Write a function expand(s1,s2) that expands shorthand notations like
       a-z in the string s1 into the equivalent complete list abc...xyz in s2.
       Allow for letters of either case and digits, and be prepared to handle
       cases like a-b-c and a-z0-9 and -a-z. Arrange that a leading or
       trailing - is taken literally. */
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    void expand( char * /*, char * */);
    char * ret_range_str( char , char );
    int is_valid_char( char );
    int main( int argc , char **argv )
    {
        if( argc != 2 )
            printf( "Usage: %s [shorthand notation]\n" , argv[ 0 ] );
        else{
            char * expanded;
            expand( argv[ 1 ] /*, expanded */);
            printf( "\n" );
            getchar();
        }
        return 0;
    }
    void expand( char * string /*, char main_string */){
        int i = 0 , range_done = 0 , range_cont = 0 , str_len = strlen( string );
        char start , end , * expanded;
        
        while( i < str_len ){
            if( string[ i ] == '-' && ( i == 0 || i == ( str_len - 1 ) ) )
                printf( "-" );    // leading or trailing '-'
            else    /* parsing for "upper ranges" */
                if( ( start = string[ i ] ) <= 'Z' && start >= 'A' ){
                    if( string[ i + 1 ] == '-' ){    // "upper-" found, checking data type of 2nd operand
                        if( ( end = string[ i + 2 ] ) <= 'Z' && end >= 'A' ){    // "upper-upper" found
                            printf( "%s" , ret_range_str( start , end )  );
                            if( string[ i + 3 ] == '-' )
                                range_cont++;
                            else
                                range_done++;
                        }
                        else{
                            if( is_valid_char( string[ i + 2 ] ) )
                                printf( "\n[!] mis-matched operands: %c%c\n" , start , end );
                            else{
                                printf( "\n[!] invalid syntax, try ./expand --usage for examples: ABORT!\n" );
                                break;
                            }
                        }
                    }
                    else{
                        printf( "\n[!] invalid syntax, try ./expand --usage for examples: ABORT!\n" );
                    }
                }else /* parsing for "lower ranges" */
                    if( ( start = string[ i ] ) <= 'z' && start >= 'a' ){
                        if( string[ i + 1 ] == '-' ){    // "lower-" found, checking data type of 2nd operand
                            if( ( end = string[ i + 2 ] ) <= 'z' && end >= 'a' ){    // "lower-lower" found
                                printf( "%s" , ret_range_str( start , end ) );
                                if( string[ i + 3 ] == '-' )
                                    range_cont++;
                                else
                                    range_done++;
                            }
                            else{
                                if( is_valid_char( string[ i + 2 ] ) )
                                    printf( "\n[!] mis-matched operands: %c%c\n" , start , end );
                                else{
                                    printf( "\n[!] invalid syntax, try ./expand --usage for examples: ABORT!\n" );
                                    break;
                                }
                            }
                        }
                        else{
                            printf( "\n[!] invalid syntax, try ./expand --usage for examples: ABORT!\n" );
                        }
                    }
                    else    /* parsing for "digit ranges" */
                        if( ( start = string[ i ] ) <= '9' && start >= '0' ){
                        if( string[ i + 1 ] == '-' ){    // "digit-" found, checking data type of 2nd operand
                            if( ( end = string[ i + 2 ] ) <= '9' && end >= '0' ){    // "digit-digit" found
                                printf( "%s" , ret_range_str( start , end ) );
                                if( string[ i + 3 ] == '-' )
                                    range_cont++;
                                else
                                    range_done++;
                            }
                            else{
                                if( is_valid_char( string[ i + 2 ] ) )
                                    printf( "\n[!] mis-matched operands: %c%c\n" , start , end );
                                else{
                                    printf( "\n[!] invalid syntax, try ./expand --usage for examples: ABORT!\n" );
                                    break;
                                }
                            }
                        }
                        else{
                            printf( "\n[!] invalid syntax, try ./expand --usage for examples: ABORT!\n" );
                            break;
                        }
                    }
                    else
                        if( string[ i ] != '-' ){
                            printf( "\n[!] invalid syntax, try ./expand --usage for examples: ABORT!\n" );
                            break;
                        }
                            
            if( range_cont ){
                i += 2;
                range_cont = 0;
            }
            else
                if( range_done ){
                    i += 3;
                    range_done = 0;
                }
            else
                i++;
        }
        return;
    }
    char * ret_range_str( char start , char end ){
        char * expanded = malloc( ( sizeof( char ) * (  ( start < end ) ? ( end - start ) : ( start - end )  ) ) + 1 );
        int offset;
        if( start < end ) /* fowards */
            for( offset = 0 ; start <= end; ++start ){
                sprintf( expanded + offset, "%c" , start );
                ++offset;
            }
        else if( start > end )    /* backwards */
            for( offset = 0 ; start >= end; --start ){
                sprintf( expanded + offset , "%c" , start );
                ++offset;
            }
        else
            sprintf( expanded , "%c" , start );
        return expanded;
    }
    int is_valid_char( char c ){
        return ( ( c >= '0' && c <= '9' ) || ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) ? 1 : 0;
    }
    edit: I also removed all the strchr bull-dookey i had in my previous code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 20
    Last Post: 03-08-2011, 05:16 PM
  2. Determining external path length of a tree
    By kolistivra in forum C Programming
    Replies: 11
    Last Post: 09-01-2006, 10:22 AM
  3. Determining Length of Keypress
    By Nascent in forum C++ Programming
    Replies: 3
    Last Post: 11-30-2005, 03:37 PM
  4. String length
    By Kokila in forum C++ Programming
    Replies: 4
    Last Post: 12-19-2001, 12:42 AM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM

Tags for this Thread