We can use inbuilt strcpy() function to copy one string to other but here, this program copies the content of one string to another manually without using strcpy() function.
Approach : Here we are giving one string in input and then with the help of for loop we transfer the content of first array to the second array.
Error : If destination string length is less than source string, entire string value won’t be copied into destination string.
For example, consider destination string length is 20 and source string length is 30. Then, only 20 characters from source string will be copied into destination and remaining 10 characters will be truncated.
// CPP program to copy one string to other // without using in-built function #include <stdio.h> int main() { // s1 is the source( input) string and s2 is the destination string char s1[] = "GeeksforGeeks", s2[100], i; // Print the string s1 printf("string s1 : %s\n", s1); // Execute loop till null found for (i = 0; s1[i] != '\0'; ++i) { // copying the characters by // character to str2 from str1 s2[i] = s1[i]; } s2[i] = '\0'; // printing the destination string printf("String s2 : %s", s2); return 0; } |
Output:
string s1 : GeeksforGeeks String s2 : GeeksforGeeks
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Why strcpy and strncpy are not safe to use?
- strcpy in C/C++
- Print substring of a given string without using any string function and loop in C
- C program to print a string without any quote (singe or double) in the program
- C program to Compare Two Strings without using strcmp() function
- C program to copy contents of one file to another file
- Program to copy the contents of one array into another in the reverse order
- How to find length of a string without string.h and loop in C?
- Write a C program to print "Geeks for Geeks" without using a semicolon
- C Program to print numbers from 1 to N without using semicolon?
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?
- C program to print characters without using format specifiers
- Program to find LCM of 2 numbers without using GCD
- C/C++ program to print Hello World without using main() and semicolon
- Can we access private data members of a class without using a member or a friend function?
- When is copy constructor called?
- When should we write our own copy constructor?
- Advanced C++ | Virtual Copy Constructor
- Copy Constructor in Java
- Copy elision in C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

