Read a string from the user and copy the string into another string using a recursive user-defined function.
Program:
The source code to copy a string to another string using recursion is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.
// C program to copy a string to another string
// using recursion
#include <stdio.h>
void copyRec(char str1[], char str2[])
{
static int i = 0;
str1[i] = str2[i];
if (str2[i] == '\0')
return;
i = i + 1;
copyRec(str1, str2);
}
int main()
{
char str1[64];
char str2[64];
printf("Enter string: ");
scanf("%[^\n]s", str1);
copyRec(str2, str1);
printf("Str1: %s\n", str1);
printf("Str2: %s\n", str2);
return 0;
}
Output:
Enter string: Hello, world!
Str1: Hello, world!
Str2: Hello, world!
Explanation:
In the above program, we created two functions copyRec() and main(). The copyRec() is a recursive function, which is used to copy a string to another string.
In the main() function, we read the value of string str1 from the user. Then we called the copyRec() function to copy one string to another string and printed the result on the console screen.
Read a string from the user and copy the string into another string using a recursive user-defined function.
Program:
The source code to copy a string to another string using recursion is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.
Output:
Explanation:
In the above program, we created two functions copyRec() and main(). The copyRec() is a recursive function, which is used to copy a string to another string.
In the main() function, we read the value of string str1 from the user. Then we called the copyRec() function to copy one string to another string and printed the result on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer