/*
* Compare 2 strings s1, s2
* s1 == s2 return 0
* s1 < s2 return < 0
* s2 > s1 return > 0
* Strings are compared according to
* conventional lexicographical ordering.
*/
int
my_strcmp(char *s1, char *s2) {
//loop until we hit the end of one of the strings
while(*s1 && *s2) {
if(*s1 < *s2)
return -1;
if(*s1 > *s2)
return 1;
//these characters were equal, go to the next
s1++;
s2++;
}
//handle equal strings
if(*s1 == *s2) {
return 0;
}
//handle s1 being shorter
if(!*s1)
return -1;
//s2 must be the shorter one
return 1;
}
/*
* Copy the string from src to dest.
*/
void my_strcpy(char *dest, char *src) {
while(*src)
*(dest++) = *(src++);
//add the null terminator
*dest = '\0';
}
/*
* Concatenate src onto dest.
* Translate: stick src onto the end dest.
*/
void my_strcat(char *dest, char *src) {
//find the end of dest;
while(*dest) dest++;
//do a copy
my_strcpy(dest, src);
}