Skip to content

Instantly share code, notes, and snippets.

@mitchellh
Created February 8, 2009 01:08
Show Gist options
  • Save mitchellh/60130 to your computer and use it in GitHub Desktop.
Save mitchellh/60130 to your computer and use it in GitHub Desktop.
// CODE FOR REFERENCE:
int lowest_alpha_string(char **strings, int n) {
if (n == 0) return -1;
int next = 1 + lowest_alpha_string(strings + 1, n - 1);
if (next == -1 || strings[0] < strings[next])
return 0;
else
return next;
}
/*
YOUR STARTING ARRAY:
array = "racecar", "streets", "track", "computer"
READ THE FOLLOWING BACKWARDS:
lowest_alpha_string(array, 4)
=> Element 0 is "racecar"
=> next is 2, which is "track"
=> Compare the two, "track" is shorter, return 2. Done.
lowest_alpha_string(array + 1, 3)
=> Element 0 is "streets"
=> next is 1 which is "track"
=> Compare two, "track" is shorter, so return next which is 1
lowest_alpha_string(array + 2, 2)
=> Element 0 is "track"
=> next is 1 ("computer"). Compare two strings, return 0, "track" is shorter
lowest_alpha_string(array + 3, 1) => Returns 0 ("computer" is only element, so (if next == -1) catches it
lowest_alpha_string(array + 4, 0) => Returns -1 (empty array)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment