compare function ignoring leading white space

This is a discussion on compare function ignoring leading white space within the C Programming forums, part of the General Programming Boards category; Hi there, I'm trying to write a compare function to be used in a qsort of a large array of ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    6

    compare function ignoring leading white space

    Hi there,

    I'm trying to write a compare function to be used in a qsort of a large array of strings.

    It has to compare, but ignore the leading white space, and only the leading white space.

    This is what I have so far, but it is crashing.

    Code:
    int cmpwhiteSpaceAsc(const void *p, const void *q)
    {    
        
        char **pp = (char**)p;
        char **qq = (char**)q;
        while(isspace(*pp))
            pp++;
        return strcmp(*pp, *qq);
    }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    http://www.thinkage.ca/english/gcos/...ib/isspac.html
    isspace takes a character as a parameter.
    Also, your function has a char** which is an array of strings. So you want to check for every string, what you do has no sense. For one string you would do:
    Code:
    int cmbString(const void *p, const void *q)
    {    
        
        char *pp = (char*)p;
        char *qq = (char*)q;
        int i, j;
        for (int i = 0; isspace(pp[i]); ++i) ;
        for (int j = 0; isspace(qq[i]); ++j) ;
        return strcmp(pp + i, qq + j);
    }
    Which skips all whitespaces for both strings p and q

    edit: You will want to compare a string and not an array of strings in a quick sort algorithm. Each string will be compared and sorted accordingly. The compare function will accept a string (char*) and the qsort algorithm an array of strings (char**)
    Last edited by C_ntua; 11-22-2008 at 07:23 PM.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    This is for a homework assignment. I guess I should explain the whole assignment. We input any number of text files and fgets to store to one large array of strings, line by line, dynamically allocating memory as needed.

    We then qsort the entire array using command line options.

    So this function sorts this large array of strings (lines) and sorts it in ascending order, but ignores the leading white space only, so:

    space A quick brown fox.
    A quick brown fox.

    would both be equal. The function is not supposed to consider white space between words, just the leading white space at the beginning of each line. Wouldn't the version you suggested also ignore the white space between words?

    Sorry I'm a c novice and still learning. This is an excercise in void* and double pointers, and qsort. Thanks for your help.
    Last edited by mc72; 11-23-2008 at 12:26 PM.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    Quote Originally Posted by C_ntua View Post
    http://www.thinkage.ca/english/gcos/...ib/isspac.html
    isspace takes a character as a parameter.
    Also, your function has a char** which is an array of strings. So you want to check for every string, what you do has no sense. For one string you would do:
    Code:
    int cmbString(const void *p, const void *q)
    {    
        
        char *pp = (char*)p;
        char *qq = (char*)q;
        int i, j;
        for (int i = 0; isspace(pp[i]); ++i) ;
        for (int j = 0; isspace(qq[i]); ++j) ;
        return strcmp(pp + i, qq + j);
    }

    edit: You will want to compare a string and not an array of strings in a quick sort algorithm. Each string will be compared and sorted accordingly. The compare function will accept a string (char*) and the qsort algorithm an array of strings (char**)

    The above code doesn't seem to work for me. Also as you mentioned, it seems to be checking for all white space in the string, not just leading white space.

    In my code, *pp is a char*(a string) in string 1, **pp is a char in the string.

    So I want to compare that string with the next string in the array, and sort them in ascending order, ignoring the white space only at the beginning of the string. But obviously I'm having some trouble understanding it.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > int i, j;
    > for (int i = 0; isspace(pp[i]); ++i) ;
    > for (int j = 0; isspace(qq[i]); ++j) ;
    Ha, guess which i and j you're using in the strcmp which follows.

    Answer - the uninitialised ones.

    The i and j declared inside the for loops have no scope!

    Other than that, seems like a good plan.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int cmbString(const void *p, const void *q)
    {    
        
        const char *pp = (const char*)p;
        const char *qq = (const char*)q;
        int i, j;
        for (i = 0; isspace(pp[i]); ++i) ;
        for (j = 0; isspace(qq[i]); ++j) ;
        return strcmp(pp + i, qq + j);
    }
    The above should be const correct, and also fix the problem Salem points out [well spotted].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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