ADVERTISEMENT
ADVERTISEMENT

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

C Strings User-defined Functions Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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 some rights reserved.