Using strcpy()
Part of the Logic & Flow section of Coddy's C journey — lesson 16 of 63.
You've learned how to find the length of strings with strlen(), but what if you need to copy the contents of one string into another? In C, you cannot use the assignment operator (=) to copy strings like you would with regular variables.
The reason is that strings are character arrays, and when you use = with arrays, you're only copying the memory address, not the actual content. This is where the strcpy() function from the <string.h> library becomes essential.
#include <stdio.h>
#include <string.h>
char source[] = "Hello";
char destination[20];
strcpy(destination, source); // Copies "Hello" into destinationThe strcpy() function takes two arguments: the destination array (where you want to copy to) and the source string (what you want to copy). It copies each character from the source to the destination, including the null terminator.
It's crucial that your destination array is large enough to hold the entire source string plus the null terminator. If the destination is too small, strcpy() will write beyond the array boundaries, causing undefined behavior.
This function is fundamental for string manipulation in C, allowing you to duplicate strings, initialize string variables with existing content, and prepare strings for further processing.
Challenge
EasyWrite a C program that demonstrates string copying using the strcpy() function. Your program should:
- Read an integer
nrepresenting the number of string operations to perform - For each operation:
- Read a source string from input
- Declare a character array named
destinationwith 100 elements - Use
strcpy()to copy the source string into the destination array - Print both the original source string and the copied destination string
- After all operations, demonstrate that the copied strings are independent by:
- Modifying the first character of the last destination array to 'X'
- Printing the modified destination string to show it has changed
Remember to include the <string.h> header to use the strcpy() function.
Your output should display the results in the following format:
Source: [source_string]
Destination: [copied_string]
Source: [source_string]
Destination: [copied_string]
...
Modified last destination: [modified_string]For example, if the input is:
3
hello
world
programmingYour output should be:
Source: hello
Destination: hello
Source: world
Destination: world
Source: programming
Destination: programming
Modified last destination: XrogrammingThis challenge tests your understanding of using strcpy() to copy string contents from one character array to another, proper memory allocation for destination arrays, and demonstrates that copied strings are independent of their sources.
Cheat sheet
To copy strings in C, use strcpy() from the <string.h> library. You cannot use the assignment operator (=) to copy strings because it only copies memory addresses, not the actual content.
#include <stdio.h>
#include <string.h>
char source[] = "Hello";
char destination[20];
strcpy(destination, source); // Copies "Hello" into destinationThe strcpy() function takes two arguments: the destination array (where you want to copy to) and the source string (what you want to copy). It copies each character from the source to the destination, including the null terminator.
The destination array must be large enough to hold the entire source string plus the null terminator to avoid undefined behavior.
Try it yourself
#include <stdio.h>
#include <string.h>
int main() {
int n;
scanf("%d", &n);
char source[100];
char destination[100];
// TODO: Write your code here
// Use a loop to process n string operations
// For each operation:
// - Read the source string
// - Use strcpy() to copy source to destination
// - Print both strings in the required format
// TODO: After the loop, modify the first character of the last destination
// and print the modified string
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions