Saturday 11 November 2017

C Program to Copy One String to Another using Recursion


Code:

#include

void copy(char [], char [], int);

int main()
{
    char str1[20], str2[20];

    printf("Enter string to copy: ");
    scanf("%s", str1);
    copy(str1, str2, 0);
    printf("Copying success.\n");
    printf("The first string is: %s\n", str1);
    printf("The second string is: %s\n", str2);
    return 0;
}

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

Output:

Enter string to copy: executecodes
Copying success.
The first string is: executecodes
The second string is: executecodes


More C programs













100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...