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.h
header file to use thestrcpy()
function. - We declare a character array
source
and initialize it with “Hello, World!”. - We declare another character array
destination
large enough to store the copied string. - We use
strcpy(destination, source)
to copy the contents ofsource
intodestination
. - 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
source
with “Manual Copy”. - We declare another character array
destination
to store the copied string. - We use a
for
loop to copy each character fromsource
todestination
until 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
source
with “Safe Copy”. - We declare
destination
with 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.