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
void function(string str1)
{
string str2;
int i=0;
while(str1[i]!=NULL)
{
str2[i]=str1[i];
i++;
}
}
There is no such an entity as "default string class" in C++.
There are character arrays and template class std::string in C++.
I think that you meant character arrays. Then your function will look
1 2 3 4 5 6 7 8 9 10
char * CopyString( char *dest, constchar *source )
{
char *p = dest;
// the comparision is used that avoid compiler warning
// otherwise you can write simply while ( *p++ = *source++ );while ( ( *p++ = *source++ ) != '\0' );
return ( dest );
}
If template class std:;string is used then it is better to assign one string to another withou writing any function
1 2 3 4
std::string source( "Hello" );
std::string dest;
dest = source;