1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

copy one string to another??

Discussion in 'C++' started by umair37, Mar 7, 2012.

  1. umair37

    umair37 New Member

    Joined:
    Mar 7, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    how to copy one string into another using LOOPS.(i am talking about default string class)??
    the code given below does not work.
    please teach me:)
    Code:
    void function(string str1)
    {
       string str2;
       int i=0; 
       while(str1[i]!=NULL)
       {
          str2[i]=str1[i];
          i++;
       }
    }
     
    Last edited by a moderator: Mar 7, 2012
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Your chosen approach is not the correct one. Simply use the assignment operator:
    Code:
    string str1="Hello";
    string str2=str1;
    cout<<str2;
    
    Output should be "Hello".
     

Share This Page