+92 332 4229 857 99ProjectIdeas@Gmail.com

How to print array from the last using recursion (C++ Tutorial)


Print array from the last using recursion
DisplayFromLast() take 3 input parameters; first is the string to display, second is the index which refers to each location of the array starting from 0 to size, and the third is the size of the string. And it display the string from the last.
Code of DisplayFromLast()
void DisplayFromLast( char str[] , int i , int size )
{
      if ( i < size )
      {
            DisplayFromLast( str , i + 1 , size );
            cout << str[i];
      }
}
Summary of DisplayFromLast()
DisplayFromLast() is a recursive function which calls itself again and again till the current index i approaches to the size of the string.
Example (C++)
#include "stdafx.h"
#include "iostream"
#include "conio.h"

using namespace std;

void DisplayFromLast( char str[] , int i , int size )
{
      if ( i < size )
      {
            DisplayFromLast( str , i + 1 , size );
            cout << str[i];
      }
}

int main()
{
      char str[]="Saad Bin Saulat";
     
      DisplayFromLast( str , 0 , strlen( str ) );

      _getche();
      return 0;
}

Output
taluaS niB daaS

0 comments: