You are here

C copy string program

C copy string program: This program copy a string using library function strcpy, C program to copy a string without strcpy.

String copy C program

#include <stdio.h>
#include <string.h>
int main()
   char source[1000], destination[1000];
   printf("Input a string\n");
   strcpy(destination, source);
   printf("Source string:      \"%s\"\n", source);
   printf("Destination string: \"%s\"\n", destination);
   return 0;

Output of program:
C program to copy a string output

Copy string in C without using strcpy

We will create our own function to copy a string.

#include <stdio.h>
#include <string.h>
void copy_string(char [], char []);
int main() {
   char s[1000], d[1000];
   printf("Input a string\n");
   copy_string(d, s);
   printf("Source string:      \"%s\"\n", s);
   printf("Destination string: \"%s\"\n", d);
   return 0; 
void copy_string(char d[], char s[]) {
   int c = 0;
   while (s[c] != '\0') {
      d[c] = s[c];
   d[c] = '\0';

C program to copy a string using pointers

Function to copy a string using pointers.

void copy_string(char *target, char *source) {
   while (*source) {
      *target = *source;
   *target = '\0';