C++ Program to copy string without using strcpy() function


In this program, You will learn how to copy string without using strcpy() function in C++.


while (str1[i] != '\0') {

str2[i] = str1[i];

i++; }


Example: How to copy string without using strcpy() function in C++

Run it Now
#include<iostream>
using namespace std;

int main() {

    char str1[100], str2[100];
    int i = 0, k;

    cout << "Enter String value:";
    cin.getline(str1, 100);

    while (str1[i] != '\0') {
        str2[i] = str1[i];
        i++;
    }
    str2[i] = '\0';

    cout << "\nThe First String is :" << str1;
    cout << "\nThe Second Copy String is :" << str2;

    return 0;
}

Output:

Enter any String :Learn C++
The First String is :Learn C++
The Second Copy String is :Learn C++