(Actually, the fact that the string is read-only was not specified as part of the riddle.)
Here's an incomplete program:
void rev(char *s);
int main() {
char *str = "hello";
rev(str);
}
write the function rev() so that it recursivly calls itself to print the string in reverse.
Solution:
There's a 'gotcha' here. You can't modify the string, since it's a string constant, and therefore may be allocated within read-only memory. (Personally, I don't like modifying paramaters, unless that is the purpose of the function). My solution is therefore:
void rev(char *s) {
if *(s+1) rev(s+1);
printf("%c", *s);
}
Last updated on 2005-08-03 14:00:00 -0800, by Shalom Craimer
Back to Tech Journal