String Data Structure
Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’.
Declaring a string is as simple as declaring a one dimensional array. Below is the basic syntax for declaring a string in C programming language.
char str_name[size];
Function to copy string (Iterative and Recursive)
Given two strings, copy one string to other using recursion. We basically need to write our own recursive version of strcpy in C/C++
Examples:
Input : s1 = "hello" s2 = "Learnengineeringforu" Output : s2 = "hello" Input : s1 = "Learnengineeringforu" s2 = "" Output : s2 = "Learnengineeringforu"
Iterative :
Copy every character from s1 to s2 starting from index = 0 and in each call increase the index by 1 until s1 doesn’t reach to end;
Output:
Learnengineeringforu
Recursive :
Copy every character from s1 to s2 starting from index = 0 and in each call increase the index by 1 until s1 doesn’t reach to end;
Output:
Learnengineeringforu
Nyc
ReplyDelete