0

I have come across this question in a programming contest, but couldn't find the answer can anyone please help me with this???

Input<<string
Output<<reverse(string)

constraints: No loops allowed,No inbuilt functions have to be used!

5
  • 2
    No, we are not going to help you win your programming contest. Commented Mar 21, 2013 at 16:43
  • 1
    @cdhowie I don't think OP would be asking this question like that if the contest was still active. Commented Mar 21, 2013 at 16:44
  • @IvayloStrandjev It's happened in the past, and people have been disqualified as a result. Commented Mar 21, 2013 at 16:45
  • I guess the contest is over -- get back to question. seems to be interesting. These questions will hit right on the face and questions our basics Commented Mar 21, 2013 at 16:47
  • 2
    C doesn't have "inbuilt" functions. All the functions defined in the Standard are part of the 'Standard Library', not the language. Commented Mar 21, 2013 at 16:56

2 Answers 2

7

Use recursion:

#include <stdio.h>

void print_reversed(const char* str) {
  if (*str) {
    print_reversed(str + 1);
    printf("%c", *str);
  }
}

int main() {
  print_reversed("abcdef");
  printf("\n");
}
Sign up to request clarification or add additional context in comments.

7 Comments

While it is probably the answer it still feels like a loop.
@FlorisVelleman Thankfully, what you feel is not in agreement with the definitions of these programming concepts. ;)
@ajp15243 But that's exactly the kind of thing I would do -- follow the letter of the constraint, not the spirit. Because it's more fun to throw the imperfect constraint back in the face of the person who wrote it than it is to follow the spirit of the constraint!
@cdhowie: I'm with you on this one. I think whoever posed the problem was trying to be too clever for their own good.
Believe me, I would love to see the judges' faces after reading a goto answer :P.
|
2

To give a concrete answer based on NPE's hint to use recursion, we need to use recursion for two things: finding the end of the string, and actually swapping everything.

Here is a complete program illustrating this approach (see it run):

#include <stdio.h>

char * find_end_of_string(char *str)
{
    return *str ? find_end_of_string(str + 1) : str;
}

void do_reverse_string(char *a, char *b)
{
    char tmp;

    if (a < b) {
        tmp = *a;
        *a = *b;
        *b = tmp;

        do_reverse_string(a + 1, b - 1);
    }
}

void reverse_string(char *str)
{
    do_reverse_string(str, find_end_of_string(str) - 1);
}

int main() {
    char odd[] = "abcde";
    char even[] = "abcdef";

    reverse_string(odd);
    reverse_string(even);

    printf("%s\n%s\n", odd, even);

    return 0;
}

2 Comments

Don't get me wrong, I am not disputing your answer's correctness.
@NPE I see what you did there. I'm used to trying to build reusable components instead of extremely task-specific stuff, so my thought process was "build something to reverse a string without using loops and print the result" instead of "build something to print a string reversed." This is why I don't do coding competitions... I'm too busy trying to be a good engineer. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.