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 character array. Next, it will use For Loop to iterate each character in that string, and copy them into another character array.

#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. The next two statements asks the user to enter any word, and assign the user entered char array to the variable. Please refer to strcpy function.

In the next line we have For Loop to iterate and copy string,

User Entered = “Good”

First Iteration: for (i = 0; Str[0]!=’\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++)
condition is TRUE because Str[1] = o
CopyStr[1] = o

Third Iteration: for (i = 2; Str[2]!=’\0′; 2++)
condition is TRUE because Str[2] = o
CopyStr[2] = o

Fourth Iteration: for (i = 3; Str[3]!=’\0′; 3++)
condition is TRUE because Str[3] = d
CopyStr[3] = d

Fifth Iteration: for (i = 4; Str[4]!=’\0′; 4++)
condition is False. So, it will exit from the For Loop

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

#include <stdio.h>
#include <string.h>
 
int main()
{
  	char Str[100], CopyStr[100];
  	int i = 0;
 
  	printf("\n Please Enter any Sentence :  ");
  	gets(Str);
 
  	while(Str[i]!='\0')
  	{
  		CopyStr[i] = Str[i];
  		i++;
  	}
  	CopyStr[i] = '\0';

  	printf("\n Copied into CopyStr = %s", CopyStr);
  	printf("\n Total Number of Characters that we copied = %d\n", i);
  	
  	return 0;
}
 Please Enter any Sentence :  Tutorial Gateway

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

Program to Copy String Using Functions

This program is the same as above. However, this time, we are using the Functions concept to separate the logic from the main.

#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 text :  ");
  	gets(Str);
 
	CopyString(Str, CopyStr, 0);
	
	printf("\n Original   = %s", Str);
  	printf("\n Copied 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 text :  C Programming Tutorial

 Original   = C Programming Tutorial
 Copied into CopyStr = C Programming Tutorial

C Program to Copy String Using Pointers

This copy program is the same as above, but this time we are using Pointers to copy one string to another.

#include <stdio.h>
#include <string.h>
void CopyString(char* st1, char* st2);

int main()
{
  	char St[100], CopyStr[100];
 
  	printf("\n Please Enter any Text :  ");
  	gets(St);
 
	CopyString(St, CopyStr);
	
	printf("\n Original   = %s", St);
  	printf("\n Copied into CopyStr = %s", CopyStr);
  	
  	return 0;
}

void CopyString(char* st1, char* st2)
{
	int i = 0;

  	for (i = 0; st1[i]!='\0'; i++)
  	{
  		st2[i] = st1[i];
  	}
	st2[i] = '\0';
}
 Please Enter any Text :  C Programs

 Original   = C Programs
 Copied into CopyStr = C Programs

Comments are closed.