Problem Statment: Write a recursive function to compare two cstrings. Similiar to the strcmp() function return an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2.
- "-1" if the first character that does not match has a lower value in s1 than in s2
- "0" the contents of both strings are equal
- "1" the first character that does not match has a greater value in s1 than in s2
int str_cmp(char const* s1, char const* s2){ if(*s1=='\0' && *s2=='\0') //if both character are null character return 0; //they must be equal if(*s1>*s2) return 1; if(*s1<*s2) return -1; return str_cmp(s1+1,s2+1); }
Comments
Post a Comment