/********************************************************************************************************************* strCompare2 This function takes two strings and compares them using recursion similarly to the strCompare function (see above). Unlike strCompare, this function ignores any non-letter characters and ignores the case of the characters. *********************************************************************************************************************/ int strCompare2(char* str1, char* str2) { // The character is not a letter, skip it. if (whatLetter(str1[0]) == -1 && str1[0] != 0) { return strCompare2(str1 + 1, str2); } if (whatLetter(str2[0]) == -1 && str2[0] != 0) { return strCompare2(str1, str2 + 1); } // Both characters are null terminators, so the strings must be equal. if ((str1[0] == 0) && (str2[0] == 0)) { return 0; } if (whatLetter(str1[0]) < whatLetter(str2[0])) // str1 is "less than" str2 { return -1; } else if (whatLetter(str1[0]) > whatLetter(str2[0])) // str1 is "greater than" str2 { return 1; } // The first letter of the two strings are equal, check rest if (whatLetter(str1[0]) == whatLetter(str2[0])) { strCompare2(str1 + 1, str2 + 1); } }
/* * same as before, only this time * ignore anything that is not a letter * ignore the case (upper case, lower case) * So, strCompare2("Hello", "hello") should return 0 (they're the same) * strCompare2("The plane!", "theater") should return 1 since "theplane" is larger than "theater" * once again, you can only use recursion, no loops */ int strCompare2(char* str1, char* str2) { if (str1[0] == str2[0]) { if (str1[0] == 0) { return 0; } } if (whatLetter(*str1) == -1) { str1++; int t = strCompare2(str1, str2); return t; } if (whatLetter(*str2) == -1) { str2++; int x = strCompare2(str1, str2); return x; } if (whatLetter(*str1) > whatLetter(*str2)) { return 1; } if (whatLetter(*str2) > whatLetter(*str1)) { return -1; } else { str1++; str2++; int z = strCompare2(str1, str2); return z; } }
/* * same as before, only this time * ignore anything that is not a letter * ignore the case (upper case, lower case) * So, strCompare2("Hello", "hello") should return 0 (they're the same) * strCompare2("The plane!", "theater") should return 1 since "theplane" is larger than "theater" * once again, you can only use recursion, no loops */ int strCompare2(char* str1, char* str2) { //End scenario if (str1[0] == 0 && str2[0] == 0) { return 0; } //Result variable int result = 0; //We jump one for the non letters if (whatLetter(str1[0]) == -1) {result = strCompare2(str1 + 1, str2);} else if (whatLetter(str2[0]) == -1) { result = strCompare2(str1, str2+1); } //We do recursion until we reach the end of both strings else if (str1[0] != 0 && str2[0] != 0) { result = strCompare2(str1 + 1, str2 + 1); } else if (str1[0] == 0) { result = strCompare2(str1, str2 + 1); } else if (str2[0] == 0) { result = strCompare2(str1 + 1, str2); } //We return previous result if current is not a letter if (whatLetter(str1[0]) == -1 || whatLetter(str2[0]) == -1) { return result; } //If past match was equal, we check if stll equal or return less or more if (result == 0) { if (whatLetter(str1[0]) == whatLetter(str2[0])) { return 0; } //Still equal else if (whatLetter(str1[0]) < whatLetter(str2[0])) { return -1; } //It is less else { return 1; } //It is more } //If currently equal, but the previous was not, we return the result else if (whatLetter(str1[0]) == whatLetter(str2[0])) { return result;} //Last case, if not equal, then check if for less or more else { if (whatLetter(str1[0]) < whatLetter(str2[0])) { return -1; } //It is less else { return 1; } //It is more } }