C program to Copy String

How to write a C program to copy string without using strcpy function. We can achieve the same in multiple ways, 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 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;
}
C program to Copy String without using strcpy 1

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 a string to Str variable. Please refer to strcpy function.

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 C Programming 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 the same as above, but this time we are using a While Loop (Just replacing For Loop with While Loop).

/* 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;
}

C string copy using a while loop output

 Please Enter any String :  Tutorial Gateway

 String that we coped into CopyStr = Tutorial Gateway
 Total Number of Characters that we copied = 16

Program to Copy String Using Functions

This string copy program is the same as above. However, this time, we are using the Functions concept to separate the logic from the 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);
}
 Please Enter any String :  C Programming Tutorial

 Original String in Str variable   = C Programming Tutorial
 String that we coped into CopyStr = C Programming Tutorial

Program to Copy String Using Pointers

This string copy program is the 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';
}
 Please Enter any String :  C Programs

 Original String in Str variable   = C Programs
 String that we coped into CopyStr = C Programs

Comments are closed.