+92 332 4229 857 99ProjectIdeas@Gmail.com

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


Print array from the start using recursion
DisplayFromFirst() 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 start.
Code of DisplayFromFirst()
void DisplayFromFirst( char str[] , int i , int size )
{
      if ( i < size )
      {
            cout << str[i];
            DisplayFromFirst( str , i + 1 , size );
      }
}
Summary of DisplayFromFirst()
DisplayFromFirst() 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 DisplayFromFirst( char str[] , int i , int size )
{
      if ( i < size )
      {
            cout << str[i];
            DisplayFromFirst( str , i + 1 , size );
      }
}

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

      _getche();
      return 0;
}

Output
Saad Bin Saulat

0 comments: