23/11/2015

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

Problem:- C++ Program to Copy One String into Another String Without Using strcpy() or Write a c++ program to copy one string to another string without library function or C Program to Copy String Without Using strcpy() or C++ Program to Copy Strings or C++ Program to Copy One String into Another String or C Program to Copy One String into Other Without Using Library Function or C++ Program to Copy One String to Another using Library Function or C++ Program to Concatenate Two Strings or strcpy in C/C++ or copy one string to another in c++.

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.


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];
}


Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.


Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page.


Try Yourself C++ Program To Print Reverse Sentence

Solution:-



#include<iostream>
using namespace std;

int main()
{
/*Visit - www.programmingwithbasics.com*/
   
    cout<<"=====================================";
    cout<<"\nVisit - www.programmingwithbasics.com";
    cout<<"\n=====================================";
    
    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:-


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



Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

1 comment: