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 Program to Reverse a String using Pointers Given a string, the task is to reverse this String using pointers. Examples:Input: GeeksOutput: skeeGInput: GeeksForGeeksOutput: skeeGroFskeeGApproach: This method involves taking two pointers, one that points at the start of the string and the other at the end of the string. The characters are then 2 min read Print Substring of a Given String Without Using Any String Function and Loop in C In C, a substring is a part of a string. Generally, we use string libary functions or loops to extract the substring from a string and then print it. But in this article, we will learn how to print the substring without using any string function or loops.The simplest method for printing a substring 2 min read Function to copy string (Iterative and Recursive) Given two strings, copy one string to another using recursion. We basically need to write our own recursive version of strcpy in C/C++ Examples: Input : s1 = "hello" s2 = "geeksforgeeks" Output : s2 = "hello" Input : s1 = "geeksforgeeks" s2 = "" Output : s2 = "geeksforgeeks" Iterative: Copy every c 6 min read Taking String input with space in C (4 Different Methods) We can take string input in C using scanf("%s", str). But, it accepts string only until it finds the first space. There are 4 methods by which the C program accepts a string with space in the form of user input.Let us have a character array (string) named str[]. So, we have declared a variable as ch 2 min read strtok() and strtok_r() functions in C with examples C provides two functions strtok() and strtok_r() for splitting a string by some delimiter. Splitting a string is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. strtok() Function The strtok() method splits str[] according 5 min read C function to Swap strings Let us consider the below program. C #include<stdio.h> void swap(char *str1, char *str2) { char *temp = str1; str1 = str2; str2 = temp; } int main() { char *str1 = "geeks"; char *str2 = "forgeeks"; swap(str1, str2); printf("str1 is %s, str2 is %s", str1, str2); ge 2 min read How to write long strings in Multi-lines C/C++? Image a situation where we want to use or print a long long string in C or C++, how to do this? In C/C++, we can break a string at any point in the middle using two double quotes in the middle. Below is a simple example to demonstrate the same. C #include<stdio.h> int main() { // We can put tw 2 min read strdup() and strndup() functions in C/C++ The strdup() and strndup() functions are used to duplicate a string. strdup() : Syntax : char *strdup(const char *s); This function returns a pointer to a null-terminated byte string, which is a duplicate of the string pointed to by s. The memory obtained is done dynamically using malloc and hence i 2 min read Print "Hello World" in C/C++ without using any header file Write a C/C++ program that prints Hello World without including any header file. Conceptually it's seems impractical to write a C/C++ program that print Hello World without using a header file of "stdio.h". Since the declaration of printf() function contains in the "stdio.h" header file. But we can 2 min read Different ways to copy a string in C/C++ Copying a string is a common operation in C/C++ used to create a duplicate copy of the original string. In this article, we will see how to copy strings in C/C++. Methods to Copy a String in C/C++1. Using strcpy() We can use the inbuilt function strcpy() from <string.h> header file to copy one 15+ min read Like