In this article, We will show you, How to write a C program to copy string without using strcpy function. We can achieve this in multiple way, but we will discuss four different approaches: using For Loop, While Loop, Functions, and Pointers
C program to Copy String without using strcpy()
This program for string copy in c allows the user to enter any string or character array. Next, it will use For Loop to iterate each and every character in that string, and copy them into another character array.
/* C program to Copy String without using strcpy() */ #include <stdio.h> #include <string.h> int main() { char Str[100], CopyStr[100]; int i; printf("\n Please Enter any String : "); gets(Str); for (i = 0; Str[i]!='\0'; i++) { CopyStr[i] = Str[i]; } CopyStr[i] = '\0'; printf("\n String that we coped into CopyStr = %s", CopyStr); printf("\n Total Number of Characters that we copied = %d\n", i); return 0; }
OUTPUT
ANALYSIS
Within this C string copy program, We declared two character arrays of size 100. Below statements asks the user to enter any word, and assign the user entered string to Str variable.
printf("\n Please Enter any String : "); gets(Str);
In the next line we have For loop to iterate and copy string,
for (i = 0; Str[i]!='\0'; i++) { CopyStr[i] = Str[i]; }
User Entered the String “Good”
First Iteration: for (i = 0; Str[0]!=’\0′; 0++)
(Str[0] != ‘\0’) condition is TRUE because Str[0] = G
CopyStr[i] = Str[i]
CopyStr[0] = Str[0] = G
Second Iteration: for (i = 1; Str[1]!=’\0′; 1++)
(Str[1] != ‘\0’) condition is TRUE because Str[1] = o
CopyStr[1] = Str[1] = o
Third Iteration: for (i = 2; Str[2]!=’\0′; 2++)
(Str[2] != ‘\0’) condition is TRUE because Str[2] = o
CopyStr[2] = Str[2] = o
Fourth Iteration: for (i = 3; Str[3]!=’\0′; 3++)
(Str[3] != ‘\0’) condition is TRUE because Str[3] = d
CopyStr[3] = Str[3] = d
Fifth Iteration: for (i = 4; Str[4]!=’\0′; 4++)
(Str[4] != ‘\0’) condition is False. So, it will exit from the For Loop
Final printf statement will print the string inside a CopyString character array
printf("\n String that we coped into CopyStr = %s", CopyStr);
C program to Copy String Using While Loop
This program for string copy in c is same as above but this time we are using While loop (Just replacing For Loop with While Loop). I suggest you to refer While Loop to understand the loop Iteration.
/* C program to Copy String without using strcpy() */ #include <stdio.h> #include <string.h> int main() { char Str[100], CopyStr[100]; int i = 0; printf("\n Please Enter any String : "); gets(Str); while(Str[i]!='\0') { CopyStr[i] = Str[i]; i++; } CopyStr[i] = '\0'; printf("\n String that we coped into CopyStr = %s", CopyStr); printf("\n Total Number of Characters that we copied = %d\n", i); return 0; }
OUTPUT
C Program to Copy String Using Functions
This string copy in c program is same as above but this time we are using Functions concept to separate the logic from main program.
/* C program to Copy String without using strcpy() */ #include <stdio.h> #include <string.h> void CopyString(char str1[], char str2[], int index); int main() { char Str[100], CopyStr[100]; printf("\n Please Enter any String : "); gets(Str); CopyString(Str, CopyStr, 0); printf("\n Original String in Str variable = %s", Str); printf("\n String that we coped into CopyStr = %s", CopyStr); return 0; } void CopyString(char str1[], char str2[], int index) { str2[index] = str1[index]; if(str1[index] == '\0') { return; } CopyString(str1, str2, index + 1); }
OUTPUT
C Program to Copy String Using Pointers
This string copy in c program is same as above but this time we are using Pointers to copy one string to another.
/* C program to Copy String without using strcpy() */ #include <stdio.h> #include <string.h> void CopyString(char* str1, char* str2); int main() { char Str[100], CopyStr[100]; printf("\n Please Enter any String : "); gets(Str); CopyString(Str, CopyStr); printf("\n Original String in Str variable = %s", Str); printf("\n String that we coped into CopyStr = %s", CopyStr); return 0; } void CopyString(char* str1, char* str2) { int i = 0; for (i = 0; str1[i]!='\0'; i++) { str2[i] = str1[i]; } str2[i] = '\0'; }
OUTPUT
Thank You for Visiting Our Blog