Re: Is there anything wrong with the code?
Quote:
Originally Posted by dullboy
Regarding 2), if I pass char*, the program still works. Here is the updated code.
Code:
int reverseString(const char* str1, char* str2)
{
char* p;
int len;
len = strlen(str1);
p =const_cast<char*>(str1)+len-1;
while(p>=str1)
{
*str2++ = *p--;
}
*str2 = '\0';
return 1;
}
int main()
{
const char* str = "My name is dullboy";
char* str2;
str2 = new char[100];
reverseString(str, str2);
cout<<str2<<endl;
delete[] str2;
return 0;
}
Check the defintion of reverseString,
int reverseString(const char* str1, char* str2). I passed char* not char** or char* & and I still get correct str2. Thanks for your inputs.
In your original post (which is what I was commenting on), you did the
"new" in reverseString , not in main.
Re: Is there anything wrong with the code?
const_cast !!!
Completely unnecessary.
Code:
int reverseString(const char* str1, char* str2)
{
const char* p;
int len;
len = strlen(str1);
p =str1+len-1;
while(p>=str1)
{
*str2++ = *p--;
}
*str2 = '\0';
return 1;
}