Google Add

Search

C Program to Copy one String into another using Recursion

Write a c program to copy one string into another using recursion.

In this program we are not using any in-built function for copy one string into another. To copy one string into another we need three arguments str1, str2, index.

str1 - Input string value.

str2 - String in which value need to be copy.

index - index value.

C Program to copy one string into another

C, C++ Interview questions


C Program to Copy one String into another using Recursion

#include <stdio.h>

void copy_str(char str1[],char str2[],int index){

      str2[index] = str1[index];
           
      /* if it reaches at the end of string. */
           
      if(str1[index]=='\0'){
          return;
       }
 
      /* Recursively Call func with incremented index value. */
      
      copy_str(str1,str2,index+1);
}

int main() {
 
 char str1[100],str2[100];
 
 
  /* Take input string. */
 
  printf("Enter string \n");
  gets(str1);
 
  /* call copy string method. */
 
  copy_str(str1,str2,0);
 
  printf("Copy of first string into second is %s",str2);
 
  return 0;
}

Output:

Enter String : cquestions.in

Copy of first string into second is : cquestions.in

No comments:

Post a Comment