In this tutorial you will learn about the C Program to Copy String and its application with practical example.
C Program to Copy String
In this tutorial, we will learn to create a C program that will Copy String in C programming.
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- Using Strings in C Programming.
What is a string?
As we all know, the string is a collection of characters, symbols, digits, and blank spaces. In strings, only one variable is declared which can store multiple values.
1. Declaring the variables for the program.
2. Add a string to the variable
3. Copy from the taken variable.
4. Printing the result string.
Program to Copy String
First, will take the input string from the user. And then will copy the string. The C programming language has many pre-defined functions for string manipulation. In today’s tutorial, we will do copy work with the help of a strcpy() function. It is used for copying the string from one variable to another variable.
With the help of the below program, we can Copy String.
/* Declaring the variable for the program required to complete the operation */
char var1, var2;
/* Taking the input string from the user for the program */
printf("Input a string\n");
/* Copying the string with the help of a predefined function
/* Printing the string to the user in output */
printf("var1 string: %s\n", var1);
printf("var2 string: %s\n", var2);
In the above program, we have first initialized the required variable.
- var1 = it will hold the string value.
- var2 = it will hold the string value.
Taking input string from the user for copying the string.
Copying the string with the help of the assignment operator.
Printing the output copied string1 and string2.