To find a recursive solution, one fruitful approach is often to begin by thinking about how the problem to be solved can be split into smaller and simpler subproblems.
One way of thinking about the problem of printing a list of strings in reverse is to consider that, given such a list, the list can be split into the first string and a list of the remaining strings. Now the list of remaining strings needs to be printed in reverse, after which the first string should be printed. When the list of strings is empty, there is nothing to reverse and nothing to print.
Here is an example rev_print() function. Here str_list is an array of pointers to char, which is terminated with a null pointer. Note that argv is such an array, and this is a convenient way to control iteration over the array.
void rev_print(char **str_list)
{
if (*str_list) {
rev_print(str_list + 1); // reverse print the rest of the list
puts(*str_list); // print the first string
}
}
If it is desired to reverse-print both the list of strings, and the characters within each string, the same approach can be applied. A function, such as rev_print_string(), can be written that splits a string into its first character and the remaining characters. The remaining characters are printed in reverse, after which the first character is printed. When the string is empty, there are no characters to reverse and print.
This function can be used in a rev_rev_print() function, similar to rev_print(). The only difference here is that instead of printing the strings with puts(), the strings are printed with rev_print(). Note that the rev_print() function does not print a newline character, so this is printed in rev_rev_print() after reverse-printing a string.
Here is a complete example program:
#include <stdio.h>
void rev_print(char **str_list);
void rev_rev_print(char **str_list);
void rev_print_string(char *str);
int main(int argc, char *argv[])
{
for (int i = 1; i < argc; i++) {
puts(argv[i]);
}
char **strings = argv + 1;
putchar('\n');
rev_print(strings);
putchar('\n');
rev_rev_print(strings);
return 0;
}
void rev_print(char **str_list)
{
if (*str_list) {
rev_print(str_list + 1); // reverse print the rest of the list
puts(*str_list); // print the first string
}
}
void rev_rev_print(char **str_list)
{
if (*str_list) {
rev_rev_print(str_list + 1); // reverse-reverse print remainder
rev_print_string(*str_list); // reverse print the first string
putchar('\n');
}
}
void rev_print_string(char *str)
{
if (*str) {
rev_print_string(str + 1); // reverse print remainder of string
putchar(*str); // print the first character
}
}
And here is a sample interaction:
λ> ./reverse It is a beautiful day
It
is
a
beautiful
day
day
beautiful
a
is
It
yad
lufituaeb
a
si
tI
./myProgram.x < It is a beautiful day, the<will be captured by the shell and interpreted as a redirection. Don't use it.rev_print(argv, argc - 1)and then printp[n]and call recursively withn - 1as long asn >= 0. It's much simpler than you made it, and you don't need static variables.&(*p)is an interesting way to writep.void rev_print(char **argv){ if (*argv) { rev_print(argv+1); puts(*argv); } }.staticvariableito hold the recursion depth. Pass the recursion depth as a parameter instead.