tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 C Language > Basic > String copy

String copy 

This example shows using strcpy library function.

File Name  :  
source/C/basic/string_copy.c 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
01#include<stdio.h>
02#include<string.h>
03#include<stdlib.h>
04 
05int main()
06{
07    int i;
08    char *first ="ONE";
09    char *second="TWO";
10    char *temp;
11 
12    printf("First string : %s", first);
13    printf("\nSecond string : %s", second);
14 
15    //Copy second to first
16    temp = first;
17    first = (char*) malloc(strlen(second) + 1);
18    strcpy(first, second);
19     
20    //Copy first to second
21    second = (char*) malloc(strlen(temp) + 1);
22    strcpy(second, temp);
23     
24    printf("\n\nFirst string : %s", first);
25    printf("\nSecond string : %s", second);
26         
27    free(second);
28    free(first);
29         
30    getch();
31    return 0;
32}

It gives the following output,
First string : ONE
Second string : TWO

First string : TWO
Second string : ONE



 
  


  
bl  br