Comparing strings with user-created string class
- by meepz
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
cs1++;
i++;
}
for my return I basically have
if(cs1>cs2){
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)