anon
anon

Reputation:

How can I use recursion to reverse the user input?

I understand that this is a very trivial problem.

I am trying to write a program that will keep asking for user input as an int until the user enters 0 at which point it will terminate and print out all the digits in reverse. This is what I have so far:

int revList(int num)
{
    if (num != 0)
    {
        scanf("%d", &num);
        printf("%d\n", revList(num));
        return num;
    }
    return 0;
}

with my main method being:

int revList(int);

printf("%d", revList(1));

the 1 in the revList was chosen arbitrarily to satisfy the compiler's need for an arguement even though it's useless in the first iteration.

I understand that my code is probably inefficient and not making use of recursion properly.

The code works but has 1 problem: it prints 0 two times and then the preceding numbers. I realize where the problem stems from. It's because I have a return 0 at the end but I can't take that out because it will reach the end of a non-void function and in this manner, it returns 0 twice.

Can someone please help me fix this and give me an efficient method to address this problem?

EDIT: example: user inputs: 1 2 3 4 5 8 0

it should output:

0 8 5 4 3 2 1

my program will output: 0 0 8 5 4 3 2 1

Upvotes: 0

Views: 1917

Answers (4)

nullseifer
nullseifer

Reputation: 112

void revList()
{
 int num = 0;
 scanf("%d", &num);

 if (num != 0)
     revList();

 printf("%d\n", num);       
}

I'll explain a little if you want to know the reasoning.

Firstly, the function doesn't need to return an integer because you're collecting the number from the user and printing it in all in one place - there's simply no need.

I'm guessing that you want 0 to be a sort of "kill command", and thus, you don't want to print it - hence why we moved the call to printf inside the if statement.

Edit:

As per you edit, you make it clear that you do want to print the collected 0. Code changed accordingly.

Upvotes: 2

Rudolfs Bundulis
Rudolfs Bundulis

Reputation: 11934

You could do it like this avoiding the dummy argument, just pass in the first digit and if it's not zero do a reucursion until zero is reached and then print the actual digit in each recursion level when previous level returns:

#include <stdio.h>
#include <stdlib.h>

int revList(int nValue)
{
    if(nValue != 0)
    {
        int nNextValue;
        scanf("%d", &nNextValue);
        revList(nNextValue);
        printf("%d", nValue);
    }
    return nValue;
}

int main(int argc, char** argv)
{
    int nValue;
    scanf("%d", &nValue);
    revList(nValue);
    return EXIT_SUCCESS;    
}

Upvotes: 0

Jarod42
Jarod42

Reputation: 217085

You may try:

void revList()
{
    int num;
    scanf("%d", &num);

    if (num != 0)
    {
        revList();
    }
    printf("%d\n", num);
}

Upvotes: 0

Slava
Slava

Reputation: 44238

void revList()
{
   int num = 0;
   if( std::cin >> num ) {
      if( num ) 
         revList();
      std::cout << num << std::endl;
   }
}

Upvotes: 1

Related Questions