Home » C programs » C String User Defined Functions

C program to copy one string into another



In this program we are implementing our own strcpy() function, here we will learn how to copy one string to another without using library function?

In this program, we will read a string and copy the string into another using stringCopy() function which is implemented by own.

program to copy one string to another (implementation of strcpy) in C

#include <stdio.h>
 
/********************************************************
    *   function name       :stringCpy
    *   Parameter           :s1,s2 : string
    *   Description         : copies string s2 into s1
********************************************************/
void stringCpy(char* s1,char* s2);
 
int main()
{
    char str1[100],str2[100];
    
    printf("Enter string 1: "); 
    scanf("%[^\n]s",str1);//read string with spaces
    
    stringCpy(str2,str1);
    
    printf("String 1: %s \nString 2: %s\n",str1,str2);
    return 0;
}
 
/******** function definition *******/
void stringCpy(char* s1,char* s2)
{
    int i=0;
    while(s2[i]!='\0')
    {
        s1[i]=s2[i];
        i++;
    }
    s1[i]='\0'; /*string terminates by NULL*/
}

Output

Enter string 1:  Help in Programming
String 1:  Help in Programming
String 2:  Help in Programming





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.




Comments and Discussions





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.