Q:

C++ Program to Copy One String into Another String Without Using strcpy()

0

Logic:- 

For this problem, you can use two methods one method is Using Library Function and secondly is Without Using Library Function. Here we are going through second method copy one string to in another without using inbuilt functions for that we are taking an input from the user after that with the help of "For Loop" copying character by character fro one string to another. Below are both methods(using library function and without using library function) you can try both.


Using Library Function

strcpy:- You can use strcpy() for copy one string to another string syntax is given below

Syntax:-

strcpy (destination, source)

it will copy a string from source and paste into a string in the destination.


Without Using Library Function

Here you can use a loop(For Loop And While Loop recommended ) and copy character by character from one string to another string.

Syntax:-

for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<iostream>
using namespace std;

int main()
{
    char s1[100], s2[100], i;
  
    cout<<"\n\nEnter The String S1: ";
    cin>>s1;
  
    for(i=0; s1[i]!='\0'; ++i)
    {
      s2[i]=s1[i];
    }
   
  s2[i]='\0';
   
    cout<<"\n\nCopied String S2 is : "<<s2;
 
 return 0;
}

 

Output:

Enter The String S1: mohamad

Copied String S2 is : mohamad

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program to Display String From Backward... >>
<< C Program to Compare Two Strings Without Using str...