To copy the contents of one string to another string

Thursday, December 4th, 2008

This is the simple implementation of the “Copy” function of the computer world. This program copies the content of one string to another.

Logic : The program asks the user to input the string to copy and stores this as str1. The inner function “stcpy” takes 2 strings as arguments. By keeping the length as the reference, it traces till EOL , for each i of this iteration, it copies the i-th  letter to the destination string, i.e str2.

After the EOL, it puts a NULL to the second line. That gives the duplicated string of the entered.

#include<stdio.h>
#include<conio.h>
void stcpy(char str1[50], char str2[50]);
void main()
{
char str1[50], str2[50];
clrscr();
printf(“\n\n\t ENTER A STRING…: “);
gets(str1);
stcpy(str1,str2);
printf(“\n\t THE COPIED STRING IS…: “);
puts(str2);
getch();
}
void stcpy(char str1[50], char str2[50])
{
int i, len = 0;
while(str1[len]!=’\0′)
len++;
for(i=0;i<len;i++)
str2[i] = str1[i];
str2[i] = ‘\0′;
}
Download exe and source code here.

Avatar of Ranjith

Author Name :
Ranjith

Total : 1 Comment


One Response to “To copy the contents of one string to another string”

  1. soha sheikh says:

    mujhe koi ye program smjha do,means every step kaise kaise ho rha ek array box example me le k plz plz!
    Q-wap input a string then find out occurence of each character?
    void main()
    {
    int i ,j,k,c;
    printf(“enter a string”);
    gets(a);
    for(i=0;i<strlen(a);i++)
    {
    c=1;
    for(j=i+1;j<strlen(a);j++)

    {
    c++;
    for(k=j;k<strlen(a);k++)
    a[k]=a[k+1];
    j–;
    }
    }
    printf("\n%c=%dtimes",a[i],c);
    }
    getch();
    }

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: