Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   copy one string to another?? (http://www.go4expert.com/forums/copy-string-t27975/)

umair37 7Mar2012 19:54

copy one string to another??
 
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++;
  }
}


xpi0t0s 8Mar2012 06:34

Re: copy one string to another??
 
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".


All times are GMT +5.5. The time now is 10:25.