Copy String Using User Define Function In C Programming

In this example, we will copy user entered string into another variable with help of user defined function.

Copy string Using User Define Function In C Programming

	
#include <stdio.h>
#include <string.h>
void CopyString(char str1[], char str2[], int index);

int main()
{
        char Str[100], CopyStr[100];
    
        printf("\n Please Enter any String :  ");
        gets(Str);
    
    CopyString(Str, CopyStr, 0);
    
    printf("Original String in Str variable   = %s", Str);
        printf("\n String that we coped into CopyStr = %s", CopyStr);
        
        return 0;
}

void CopyString(char str1[], char str2[], int index)
{
    str2[index] = str1[index];
    if(str1[index] == '\0')
    {
        return; 
    }
    CopyString(str1, str2, index + 1);
}
	

Output :

	
Please Enter any String :  Hello World!
Original String in Str variable   = Hello World!
String that we coped into CopyStr = Hello World!
	

Share your thoughts

Ask anything about this examples