Comparing strings with user-created string class

Posted by meepz on Stack Overflow See other posts from Stack Overflow or by meepz
Published on 2010-03-28T23:23:08Z Indexed on 2010/03/29 0:13 UTC
Read the original article Hit count: 604

Filed under:

Basically, I created my own string class, mystring.h and mystring.c. What I want to do is write a function that compares two "strings" and then returns 1 first word is larger than the second, the opposite if word 2 is larger than word 1, and 0 if the two words are equal. What I have so far is this:

int compareto(void * S1, void * S2){
    String s1 = (String S1);
    String s2 = (String S2);
    int i, cs1 = 0, cs2 = 0; //cs1 is count of s1, cs2 is count of s2

    while(s1->c[i] != '\0'){ //basically, while there is a word
        if(s1->c[i] < s2->c[i]) // if string 1 char is less than string 2 char
            cs2++; //add to string 2 count
        else (s1->c[i] > s2->c[i]) //vice versa

for my return I basically have

         return 1;
    else if(cs2 > cs1){
         return 2;
    return 0;

here is mystring.h

typedef struct mystring {
    char * c;
    int length;

    int (*sLength)(void * s);
    char (*charAt)(void * s, int i);
    int (*compareTo)(void * s1, void * s2);
    struct mystring * (*concat)(void * s1, void * s2);
    struct mystring * (*subString)(void * s, int begin, int end);
    void (*printS)(void * s);

} string_t;
typedef string_t * String;

This does what I want, but only for unspecified order. What I want to do is search through my linked list for the last name. Ex. I have two entries in my linked list, smith and jones; Jones will be output as greater than smith, but alphabetically it isnt. (I'm using this to remove student entries from a generic link list I created) Any suggestions, all of my google searches involve using the library, so I've had no luck)

© Stack Overflow or respective owner

Related posts about c

    Related posts about homework