Copy One String to Another in C
To copy one string to another in C, we use functions like strcpy() from the string.h library or manually copy characters using loops.
Examples of Copying Strings in C
1. Copying a String Using strcpy()
In this example, we use the strcpy() function from string.h to copy a source string into a destination string.
main.c
</>
Copy
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, World!";
char destination[50];
// Copying string using strcpy()
strcpy(destination, source);
printf("Copied String: %s\n", destination);
return 0;
}
Explanation:
- We include the
string.hheader file to use thestrcpy()function. - We declare a character array
sourceand initialize it with “Hello, World!”. - We declare another character array
destinationlarge enough to store the copied string. - We use
strcpy(destination, source)to copy the contents ofsourceintodestination. - The
printf()function prints the copied string.
Output:
Copied String: Hello, World!
2. Copying a String Using a Loop
Instead of using strcpy(), we can manually copy characters from one string to another using a loop.
main.c
</>
Copy
#include <stdio.h>
int main() {
char source[] = "Hello, Arjun!";
char destination[50];
int i;
// Copying string using a loop
for (i = 0; source[i] != '\0'; i++) {
destination[i] = source[i];
}
destination[i] = '\0'; // Null-terminate the destination string
printf("Copied String: %s\n", destination);
return 0;
}
Explanation:
- We declare and initialize the character array
sourcewith “Manual Copy”. - We declare another character array
destinationto store the copied string. - We use a
forloop to copy each character fromsourcetodestinationuntil we reach the null character'\0'. - After copying all characters, we manually append the null terminator
'\0'to mark the end of the string. - Finally, we print the copied string using
printf().
Output:
Copied String: Hello, Arjun!
3. Copying a String Using strncpy()
We use strncpy() when we want to copy a limited number of characters from one string to another, ensuring buffer safety.
main.c
</>
Copy
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, Ram!";
char destination[15];
// Copying first 9 characters using strncpy()
strncpy(destination, source, sizeof(destination) - 1);
destination[strlen(source)] = '\0'; // Ensuring null termination
printf("Copied String: %s\n", destination);
return 0;
}
Explanation:
- We declare and initialize the character array
sourcewith “Safe Copy”. - We declare
destinationwith a fixed size of 15 characters. - We use
strncpy(destination, source, sizeof(destination) - 1)to copy at most 9 characters. - We manually append
'\0'at the position of length of original string to ensure the string is properly null-terminated. - Finally, we print the copied string using
printf().
Output:
Copied String: Hello, Ram!
Conclusion
In this tutorial, we explored different methods for copying strings in C:
strcpy(): The simplest way to copy strings usingstring.h.- Loop Method: Manually copying character-by-character with a loop.
strncpy(): A safer alternative tostrcpy()for preventing buffer overflow.
Each method has its own use case, and choosing the right one depends on memory safety and the size of the destination buffer.
