why cant i reverse char array using pointer??
can someone please tell me whats wron with my code?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class my_string
{
char* ptr;
int size;
public:
my_string(){};
my_string(char *str=""){size = strlen(str);ptr =str;}
my_string(const my_string &obj);
my_string& operator= (const my_string& obj);
char* getstr () {return ptr;};
void reverse();
int find (char);
void print();
~my_string(){delete [] ptr;};
};
my_string::my_string(const my_string &obj)
{
ptr=new char;
strcpy_s(ptr,50,obj.ptr);
size=obj.size;
}
my_string& my_string:: operator= (const my_string& obj)
{
strcpy_s(ptr,50,obj.ptr);
size=obj.size;
return *this;
}
void my_string::reverse()
{
for ( int i = 0; i < size/2; i++ )
{
char tmp = ptr[i];
ptr[i] = ptr[size-1-i];
ptr[size-1-i] = tmp;
}
ptr[size] = '\0';
}
int my_string::find(char c)
{
for (int i=0;i<size;i++)
{
if (ptr[i]==c)
return i;
}
return -1;
}
void my_string::print()
{
for (int i=0;i<size;i++)
cout<<ptr[i];
cout<<endl;
}
int main()
{
my_string s1("abcde");
my_string s2=s1;
s1.print();
s1.reverse();
s1.print();
}
|
What errors are you getting, and if you aren't getting errors, what is wrong exactly? Please specify, and gives us as much information as you can.
line 25: Change
ptr=new char;
To
ptr=new char[obj.size]; // assuming that obj.size includes the terminating 0 (otherwise + 1)
line 26: Why 50? Use size
instead
line 13: This will very likely lead to a crash on line 20
@NT3
im not gettin any errors but the output im gettin is :
abcde
abcde
and then the program crashes...
@coder777
i changed line 25.
line 26 when i tried changing it to size/+1 the program crashed before i got any output.
and can u plz explain why line 13 will lead to a crash in line 20?
and can u plz explain why line 13 will lead to a crash in line 20? |
The only case where you should use delete[] is on memory that has been allocated with new[].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
class my_string
{
char* ptr;
int size;
public:
// my_string(){};
// my_string(char *str="") // {size = strlen(str);ptr =str;}
my_string( const char* cstr = "" ) // also the default constructor
{
size = strlen(cstr) ;
ptr = new char[ size + 1 ] ; // allocate memory for size+1 chars;
// +1 for the null char
strcpy( ptr, cstr ) ;
}
my_string(const my_string &obj);
my_string& operator= (const my_string& obj);
char* getstr () {return ptr;};
void reverse();
int find (char);
void print();
~my_string(){delete [] ptr;};
};
my_string::my_string(const my_string &obj)
{
// ptr=new char;
ptr = new char[ obj.size + 1 ] ; // allocate sufficient memory
// strcpy_s(ptr,50,obj.ptr); // *** avoid; non-standard
strcpy( ptr, obj.ptr ) ;
size=obj.size;
}
my_string& my_string:: operator= (const my_string& obj)
{
if( this != &obj ) // check for self-assignment
{
delete[] ptr ; // do what the desructor does
// then do what the copy constructor does
ptr = new char[ obj.size + 1 ] ; // +1 for the null char
strcpy( ptr, obj.ptr ) ;
size=obj.size;
}
return *this ;
}
void my_string::reverse()
{
int left = 0 ; // position of first char
int right = size - 1 ; // position of char just before the null char
// (leave the null character where it is )
while( left < right )
{
std::swap( ptr[left], ptr[right] ) ;
++left ;
--right ;
}
}
int my_string::find(char c)
{
for (int i=0;i<size;i++)
{
if (ptr[i]==c)
return i;
}
return -1;
}
void my_string::print()
{
for (int i=0;i<size;i++)
cout<<ptr[i];
cout<<endl;
}
int main()
{
my_string s1("abcde");
my_string s2=s1;
s1.print();
s1.reverse();
s1.print();
}
|
Last edited on
working! great thank you very much!
Topic archived. No new replies allowed.