Copy One Character Array to Another Character Array

Copy One Character Array to Another Character Array” is a basic operation of character array. Here, we are given a character array of length ā€˜nā€™. Our task is to copy given character array to another without using any in-built function.

The steps required to copy given character array to another is as follows:

  1. Create a temporary character array of same size as of original character array.
  2. Scan the complete character array and copy character by character to temporary character array.

C++ Program to copy character array to another character array is as follows:

#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    char str[len];  
    cout<<"Enter the string:\n";  
    cin>>str;  
    int length = strlen(str);  
    char temp[length+1];  
    for(int i = 0; i < length; i++)  
    {  
        temp[i] = str[i];  
    }  
    temp[length] = '\0';  
    cout<<"\nThe Temporary String is: "<<temp;  
}  
OUTPUT:
Enter the string: helpmestudybro
The Temporary Array is: helpmestudybro
Time Complexity:
O(n), where n is the length of string.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *