In this tutorial you will learn about the C Program to Copy String Without Using strcpy and its application with practical example.
C Program to Copy String Without Using strcpy
In this tutorial, we will learn to create a C program that will Copy String Without Using strcpy in C programming.
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- Concepts of for loop.
- Using String functions of c language.
Copy String Without Using strcpy
As we all know the String is a collection of character data types. In strings, only one variable is declared which can store multiple values. First will take the string from the user. Then will pass that string to a for loop for copying. The C programming language has many pre-defined functions for string manipulation. but in today’s tutorial, we will do without using strcpy().
With the help of this program, we can Copy String Without Using strcpy
1. Declaring the variables for the program.
2. Add a sting to the variable
3. Print the given string.
4. Now pass thatstring to for loop for copying
5. Printing the result string.
Program to Copy String Without Using strcpy:-
char string1, string2, i;
//taking input string from the user
printf("Enter string 1 to be copied: ");
//putting string in 1 variable
fgets(string1, sizeof(string1), stdin);
//Adding that string to another variable
for (i = 0; string1[i] != '\0'; ++i)
string2[i] = string1[i];
string2[i] = '\0';
//printing output of string copied without strcpy
printf ("Copied string 2 without strcpy(): %s", string2);
In the above program, we have first initialized the required variable.
- string1 = it will hold the string value.
- string2 = it will hold the string value.
- i = it will hold the integer value for the loop.
Taking Input string from the user.
Reading the input string from the user.
Copying the string with the help of for loop without using strcpy() function.
Printing output copied string2 without strcpy.