COPYING ONE STRING INTO ANOTHER WITHOUT USING STRCPY IN C LANGUAGE
Introduction:
In this tutorial will learn how to replicate a string in the language of C into a different string. Will discuss the two types of library functions and their implementations with examples. Additionally, we will learn how to use a pointer to copy a string from one string to another. Count the number of ways we can duplicate a string to a different string. A string can be copied from one location to a different location using the C language standard library function strcpy. The header line string.h contains its definition. While the built-in strcpy() method can be used to copy a particular string to a different string, this program directly replicates the information from a specific string to a different one without the need for strcpy().
Strategies for Copying Text without Using strcpy():
As stated below, there are two ways for printing a string without utilizing the strcpy() function:
- Using for loop
- Using Pointers
- Using Functions
- Using While loop
- Using Recursion
Using for loop
Here, we are passing in a single string, and the data of the original array is then moved to the different array using a for loop.
Note: The complete string value cannot be replicated into the desired string if the length of the target string is shorter than that of the source string. Take, for instance, a scenario in which the original string is thirty and the target string is twenty. The other ten characters are subsequently shortened and just 20 characters from the original string will be transferred into the final destination.
Program:
#include <stdio.h>
int main()
{
char str_1[] = "JavaTPoint", str_2[500], j;
printf (" First String : %s \n", str_1);
for (j = 0; str_1[j] != '\0'; ++j) {
str_2[j] = str_1[j];
}
str_2[j] = '\0';
printf (" Second string : %s", str_2);
return 0;
}
Output:
Time Complexity: O(n)
Space Complexity: O(n)
Using Pointers:
Below is the program for copying a string from one location to another location using pointers in C language.
Program:
#include <stdio.h>
#include <string.h>
void CopyString (char* str_1, char* str_2)
{
int j = 0;
for (j = 0; str_1[j] != '\0'; j++)
{
str_2[j] = str_1[j];
}
str_2[j] = '\0';
}
int main()
{
char string_1[200] = "Welcome to Coding Class", string_2[100];
CopyString (string_1, string_2);
printf (" \n Original String : %s", string_1);
printf (" \n Cpoy of original string in another location : %s", string_2);
return 0;
}
Output:
Using Functions:
Below is the program to copy string into another location using while loop in C language.
Program:
#include <stdio.h>
#include <string.h>
void Copying_String (char string_1[], char string_2[], int indx);
int main()
{
char String[100], Copyed_String [100];
printf ("\n Please Enter the input string : ");
gets (String);
Copying_String (String, Copyed_String, 0);
printf (" \n Original string is : %s", String);
printf (" \n Copy of original string is : %s", Copyed_String);
return 0;
}
void Copying_String (char string_1[], char string_2[], int indx)
{
string_2[indx] = string_1[indx];
if (string_1[indx] == '\0')
{
return;
}
Copying_String (string_1, string_2, indx + 1);
}
Output:
Using Functions:
We are able to write the procedure Copying_String(). The main() function has the ability to call this function. Two strings must be supplied as arguments. Every element of the input string can be copied into a different string using the Cpoying_string() method. Below is the program to copy string into another location using while loop in C language.
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char String[100], Copying_String[100];
int j = 0;
printf ("\n Please Enter the input string : ");
gets (String);
while (String[j] != '\0')
{
Copying_String[j] = String[j];
j++;
}
Copying_String[j] = '\0';
printf ("\n Copied into the Copying_Str = %s", Copying_String);
printf ("\n Total number of characters after copying is : %d \n", j);
return 0;
}
Output:
Conclusion:
We implemented different examples and theories and have covered the various examples demonstrating how to copy one string into another without using STRCPY in C. The four approaches for that can use be used to copy string are using the for loop, using while loop, using the pointers, using the functions. We hope that reading this tutorial has helped you become more acquainted about C programming.