Function to copy string (Iterative and Recursive)


In a C++ environment, iteration statement is a compound or cause statement which can be executed zero or more than one at a time. This process is subjected to a loop termination process. The statement executes in a particular order when there is a presence of break statement or the continuation of a statement.

There are four types of iteration present in C++ -

  • While

  • Do

  • For

  • Range-based for

Every loops mentioned here iterates a particular condition until its termination. The expression evaluates as zero when the statement. The iteration statements can't be considered as declaration. In these, there is no increment will not apply to while and do statement. But, for and range-based for the increment is must to run the method.

Here are some features of iteration method −

  • Initial guesses – 1

  • Open bracket type

  • Rate of convergence is very fast

  • Convergence is a linear process

  • Approach is modifying

  • Accuracy is really good

  • Programming effort is really easy

Recursion is a particular function in C++ which calls itself directly or indirectly in a repeatation manner until the particular condition is satisfied. There is no base case for a recursive function. In recursion there are two cases to execute a code

  • Recursive condition − It helps to repeat the coding operation in a loop. It saves the time and reduce complexity of a code.

  • Base condition − It helps the condition to terminate the process.

Algorithm for iteration in C++

Here is the basic algorithm to copy a string by using iteration method by which a condition will run in a repeated manner until its termination.

  • Step 1 − Start

  • Step 2 − Read the value of x0 and e. (e is desired accuracy)

  • Step 3 − x1 = g(x0) calculation

  • Step 4 − If [x1 – x0] <= e, got for step 6

  • Step 5 − Else, assign x0 = x1 and goto step 3.

  • Step 6 − Display x1 as the root.

  • Step 7 − Stop

Algorithm for recursion in C++

Here is the recursion method algorithm to copy a string for C++ environment by which the logic condition will run until its termination.

  • Step 1 − Start

  • Step 2 − Define the base case.

  • Step 3 − Define the recursive case.

  • Step 4 − Ensure the recursion terminates.

  • Step 5 − Combine the solution.

  • Send 6 − End

Syntax to copy a function using iteration

template<class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first content, InputIterator last content, OutputIterator result content){
   while (first!=last) {
      *result = *first;
      ++result; ++first;
   }
   return result;
}

Here is this syntax, which copies the particular elements. The function returns a value as an iterator at the end of the destination.

Syntax to copy a function using recursion

int main(){
   char str10[70], str20[80];
   printf("Enter string to copy for this operation: ");
   scanf("%[^\n]s", str10);
   copy(str10, str20, 0);
   printf("Copying success for the operation.\n");
   printf("The first string is here: %s\n", str10);
   printf("The second string is here: %s\n", str20);
   return 0;
}
void copy(char str1[], char str2[], int index) {
   str20[index] = str10[index];
   
   // printf ("INDEX IS %d\n", index);
   if (str1[index] == '\0')
   return;
   copy(str10, str20, index + 1);
}

Here in this syntax to copy() a function by using recursion. The child function returns the control value to that particular parent function, encoded in the string one to be copied to string number two. The basic flow and the process mentioned below −

char* strcpy(char* destination, const char* source);
void recurse() {
   ... .. ...
   recurse();
   ... .. ...
}
int main() {
   ... .. ...
   recurse();
   ... .. ...
}
  • Define a function by "void copy (char[],char[],int);".

  • This function is used to copy one to another string by recursion.

  • Add values to the strings.

Approach

  • Approach 1 − Copy a string by using C++.

  • Approach 2 − Copy one string to another by using iterative method.

  • Approach 3 − Copy one string to another by using recursive method.

Copy a string by using by C++

Here we have used strcpy() and cstring functions to copy a sting in a C++ environment.

Example 1: Copy a sting object in a C++ environment

#include <iostream>
using namespace std;
int main() {
   string s07, s16;
   cout << "Enter data in string s07: ";
   getline (cin, s07);
   s16 = s07;
   cout << "s07 = "<< s07 << endl;
   cout << "s16 = "<< s16;
   return 0;
}

Output

Enter data in string s07: s07 = 
s16 = 

Example 2: Copy a C-string by using C++

#include <iostream>
#include <cstring>
using namespace std;
int main() {
   char s07[2022], s16[2022];
   cout << "Enter some data input in string s07: ";
   cin.getline(s07, 2022);
   strcpy(s16, s07);
   cout << "s07 = "<< s07 << endl;
   cout << "s16 = "<< s16;
   return 0;
}

Output

Enter some data input in string s07: s07 = 
s16 =

Copy one string to another by using iterative method

For iteration, we can copy every character content from s100 to s200, starting from the particular index denoted as 0. By the implementation of this method, each call increases the every index by 1. For this the s100 string doesn't reach for the termination. The time complexity is O(m), where the m is the length of the string and the auxiliary space is O(1), where extra space is being used.

Example 3

#include <bits/stdc++.h>
using namespace std;
// declare a function to override the method to a particular data string onanother void string
// lets assume we have enough space

void myCopy(char s100[], char s200[]) {
   int a = 0;
   for (a=0; s100[a] != '\0'; a++)
   s200[a] = s100[a];
   s200[a] = '\0';
}

// Declare the driver function to go further
int main() {
   char s100[2022] = "ARBRDDINDBD";
   char s200[2001] = "";
   myCopy (s100, s200);
   cout << s200;
   return 0;
}

Output

ARBRDDINDBD

Copy one string to another by using recursive method

Implementing the recursion process, we can copy each character from string s1997 to string s2001. The staring index is 0 and the method index will increase by 1 until the process terminates. For this process, the time complexity is O(m), where m is the length of a particular string. The auxiliary space here is also O(m) due to recursive call stack.

Example 4

#include <bits/stdc++.h>
using namespace std;

// Apply function to copy one string in to other string by using recursion method
void myCopy(char s1997[], char s2001[], int index = 0) {

   // copying every character from the string s1997 to string s2001
   s2001[index] = s1997[index];

   // if the operated string reachs to the end then stop and terminate the process
   if (s1997[index] == '\0')
   return;

   // increase the string characters by one to stop the termination
   myCopy(s1997, s2001, index + 1);
}

// Declare the driver function to go for the next
int main() {
   char s1997[1000] = "RDDARBINDBD";
   char s2001[1000] = "";
   myCopy(s1997, s2001);
   cout << s2001;
   return 0;
}

Output

RDDARBINDBD

Conclusion

Today in this article we have learnt how to copy a string data from one to another by using iteration and recursion in a C++ environment. Here we have mentioned the possible algorithms and built the C++ codes as per the logic. Hope this will help you to get a broad view about the discussed topic here.

Updated on: 05-Apr-2023

312 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements