Copy a String to Another using Character Arrays
In C, we can copy a string to another using character arrays by manually copying each character using a loop or utilizing built-in functions like strcpy()
. Since C does not support direct assignment of character arrays, we must copy strings character by character.
Examples of Copying Strings
1. Copying a String Using a Loop
In this example, we will copy a string from one character array to another manually using a For loop.
main.c
</>
Copy
#include <stdio.h>
int main() {
char source[] = "Hello, World!";
char destination[50]; // Large enough to hold the copied string
int i;
// Copy characters one by one
for (i = 0; source[i] != '\0'; i++) {
destination[i] = source[i];
}
destination[i] = '\0'; // Add null terminator
printf("Source String: %s\n", source);
printf("Copied String: %s\n", destination);
return 0;
}
Explanation:
- We declare a character array
source[]
containing the string “Hello, World!”. - We create an empty
destination[50]
array large enough to store the copied string. - We use a
for
loop to iterate throughsource
, copying each character todestination
. - The loop stops when the null character
'\0'
is encountered, ensuring the copied string is valid. - We manually add the null terminator
destination[i] = '\0'
at the end of the string. - We print both the source and copied strings using
printf()
.
Output:
Source String: Hello, World!
Copied String: Hello, World!
2. Copying a String Using strcpy()
In this example, we use the built-in strcpy()
function from the string.h
library to copy a string.
main.c
</>
Copy
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "C Programming";
char destination[50];
// Using strcpy() to copy the string
strcpy(destination, source);
printf("Source String: %s\n", source);
printf("Copied String: %s\n", destination);
return 0;
}
Explanation:
- We include
string.h
to use thestrcpy()
function. - We declare a character array
source[]
with the string “C Programming”. - We create an empty
destination[50]
to hold the copied string. - The
strcpy(destination, source)
function copies the contents ofsource
intodestination
. - We print both strings using
printf()
.
Output:
Source String: C Programming
Copied String: C Programming
3. Copying a String Using while
Loop
In this example, we will copy a string character by character using a while
loop.
main.c
</>
Copy
#include <stdio.h>
int main() {
char source[] = "Arjun";
char destination[50];
int i = 0;
// Using while loop to copy string
while (source[i] != '\0') {
destination[i] = source[i];
i++;
}
destination[i] = '\0'; // Add null terminator
printf("Source String: %s\n", source);
printf("Copied String: %s\n", destination);
return 0;
}
Explanation:
- We declare
source[]
with the string “Copy Example”. - An empty
destination[50]
array is allocated to hold the copied string. - We initialize
i = 0
and use awhile
loop to copy each character until we reach the null terminator. - The null terminator is manually added at the end of
destination
. - We print both strings using
printf()
.
Output:
Source String: Arjun
Copied String: Arjun
Conclusion
In this tutorial, we explored different methods to copy a string into another character array in C:
- Using a loop: Manually copying characters one by one.
- Using
strcpy()
: Built-in function for easy copying. - Using a
while
loop: Alternative method to copy strings dynamically.