Alright, that's my final version.
The HEAVY HEAVY commenting is required by the testers.
I didn't find any bugs, but if anyone does...PHP Code:// -----------------------------------------------------
// Function Name: strcmp()
//
// Input: Two strings to compare.
//
// Output: (-1) the second string is bigger.
// ( 1) the first string is bigger.
// ( 0) the strings are identical.
//
// Operation: The function receives two strings to
// compare in dictionary-comparison, and
// returns a flag according to which
// string is bigger.
// -----------------------------------------------------
int strcmp(const char* p1, const char* p2)
{
// A variable to hold the distance in ASCII charcters
// between the current two characters we're comparing.
int dist = 0;
// Keep checking while the distance is 0, and we're
// not hitting NULL on one of the strings.
while (!dist && *p1 && *p2)
// Get the distance while incrementing the
// pointers to the next character.
dist = (*p2++) - (*p1++);
// Check the last distance and according to this
// return (1) if the first string is bigger, (-1)
// if the second string is bigger, or (0) if the
// strings are identical.
if (dist > 0)
return (-1);
else if (dist < 0)
return (1);
return (0);
}





Reply With Quote