C program to copy string with or without strcpy


                                          C program to demontrate that how to copy one string into another in C Programming? There is one string function strcpy() which provide the facility to copy one string into another. The following is the syntax of string copy function.

Syntax: 
                                        strcpy(string1,string2);

Example:
                                         strcpy(str1,str2);

                                          In strcpy() function it copy second string into first one. In above example it copy str2 into str1.


C program to copy one string into another.

#include<stdio.h>

void main()
{
       int i;
       char str1[50],str2[50];
       clrscr();

       printf("Enter first string: ");
       scanf("%s",&str1);

       printf("Enter second string: ");
       scanf("%s",&str2);

       strcpy(str1,str2);

       printf("First string is: %s",str1);
       getch();

}


c program to copy string with strcpy function



C program to copy one string into another without string function

                                           To copy one string into another without using string handling function loop will be continue till the last character of the second string. and after completing loop the last character must be initialize with the null value.


#include<stdio.h>
#include<conio.h>

void main()
{
       int i;
       char str1[50],str2[50];
       clrscr();

       printf("Enter first string:");
       scanf("%s",&str1);

       printf("Enter second string:");
       scanf("%s",&str2);

       for(i=0;str2[i]!='\0';i++)
       {
              str1[i]=str2[i];
       }

       str1[i]='\0';

       printf("first string is : %s",str1);
       printf("\nSecond string copy into first one");

       getch();
}


c program to copy string without strcpy function

0 comments:

Post a Comment