Criando minha própria função strcmp () em C
Fui designado pelo meu professor para escrever meu própriostrcmp()
função em C. Criei minha própria versão da referida função e esperava obter algum feedback.
int CompareTwoStrings ( char *StringOne, char *StringTwo ) {
// Evaluates if both strings have the same length.
if ( strlen ( StringOne ) != strlen ( StringTwo ) ) {
// Given that the strings have an unequal length, it compares between both
// lengths.
if ( strlen ( StringOne ) < strlen ( StringTwo ) ) {
return ( StringOneIsLesser );
}
if ( strlen ( StringOne ) > strlen ( StringTwo ) ) {
return ( StringOneIsGreater );
}
}
int i;
// Since both strings are equal in length...
for ( i = 0; i < strlen ( StringOne ); i++ ) {
// It goes comparing letter per letter.
if ( StringOne [ i ] != StringTwo [ i ] ) {
if ( StringOne [ i ] < StringTwo [ i ] ) {
return ( StringOneIsLesser );
}
if ( StringOne [ i ] > StringTwo [ i ] ) {
return ( StringOneIsGreater );
}
}
}
// If it ever reaches this part, it means they are equal.
return ( StringsAreEqual );
}
StringOneIsLesser, StringOneIsGorgeous, StringsAreEqual são definidos como const int com os respectivos valores: -1, +1, 0.
O problema é que não tenho muita certeza se, por exemplo, meu StringOne tem um comprimento menor que meu StringTwo, isso significa automaticamente que StringTwo é maior, porque não sei comostrcmp()
é particularmente implementado. Eu preciso de alguns dos seus comentários para isso.