C program to copy string without using strcpy() function Last Updated : 15 Dec, 2024 Comments Improve Suggest changes Like Article Like Report In C, the strcpy() function is commonly used for copying strings, but in this article, we will learn how to copy strings without using strcpy() function.The simplest method to copy string without using strcpy() function is by using a loop. Let’s take a look at an example: C #include <stdio.h> void copy(char *src, char *dst) { int i = 0; // Copy each character from source to destination while (src[i] != '\0') { dst[i] = src[i]; i++; } // Null terminate destination string dst[i] = '\0'; } int main() { char src[] = "Hello, Geeks!"; char dst[50]; // Copy the string src to dest copy(src, dst); printf("%s", dst); return 0; } OutputHello, Geeks!Explanation: The most straightforward approach is to copy each character from the src string to the dest string using a loop. We continue copying characters until we encounter the null terminator ('\0'), which indicates the end of the string.There are also a few other methods in C to copy string without using strcpy() function. Some of them are as follows:Using PointersInstead of using array indexing, we can use pointers to traverse the source and destination strings. We simply move the pointer in the source string and assign its value to the destination string. C #include <stdio.h> void copy(char *src, char *dest) { // Copy each character from source to // destination using pointers while (*dest++= *src++ ); } int main() { char src[] = "Hello, Geeks!"; char dest[50]; // Copy src to dest copy(src, dest); printf("%s", dest); return 0; } OutputHello, Geeks!Using memcpy()The memcpy() function is part of the C standard library and can be used to copy a specified number of bytes from one memory location to another. This is useful for copying strings when you know their length in advance. C #include <stdio.h> #include <string.h> void copy(char *src, char *dest) { // Copy the string using memcpy memcpy(dest, src, strlen(src) + 1); } int main() { char src[] = "Hello, Geeks!"; char dest[50]; // Copy the string src to dest copy(src, dest); printf("%s", dest); return 0; } OutputHello, Geeks! Comment More infoAdvertise with us Next Article C program to copy string without using strcpy() function P Pushpanjali chauhan Follow Improve Article Tags : Misc Strings C Language Computer Science Fundamentals DSA cpp-string C-String +3 More Practice Tags : MiscStrings Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read C Programming Language Tutorial C is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used 5 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e 7 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Like