You are here:

C/c++/oop

Advertisement


Question
Dear Sir
I wrote this program to reverse a string using dynamic constructor concept but this shows a garbage output. I have written this program in dev c++.
Please help me out with this sir i really need it for my viva tomo ....


#include<iostream>
#include<cstring>
using namespace std;

class rstring
{
     int len;
     char *name;
     
     
     public:
         rstring()
         {
         len=0;
         name=new char[len+1];
         }
         
         rstring (char *s)
         {
         len=strlen(s);
         name=new char[len+1];
         strcpy(name,s);
         }
         
         void display()
         {
         
         cout<<name<<endl;
         }
         void rev();
     };
     
     void rstring::rev()
     {
         name=new char[len+1];
         strcpy(name,strrev(name));
         
     }
     /*char* strrev(char *s)
{
 int i, j;
 char *t;
 strcpy(t,s);
 for(i = 0 , j = strlen(s) - 1 ; j >= 0 ; i++, j--)
    *(s + i) = *(t + j);
 return s;
}*/
     
     int main()
     {
         char a[20];
         cout<<"enter ur name: ";
         cin>>a;
         rstring obj1,obj2;
         obj1.rev();
         cout<<"reverse of the string is :";
         obj1.display();
         system("PAUSE");
         return 0;
     }

Answer
Hello akanksha

The first problem with the code is here:
rstring obj1,obj2;
obj1.rev();

Notice that you are creating obj1, but not setting it to anything, then you are calling rev on the object. One reason for bad output is that obj1 is not properly initialized. Do this instead:
rstring obj1(a);
This calls obj1 with the input string a.

You are not using obj2, so it can be erased.

The second problem is in
void rstring::rev()
{
 name=new char[len+1];
 strcpy(name,strrev(name));       
}
The original string was stored in name. The first thing that rev does is get a new piece of memory and have name point to that new memory. Then the original name is lost. Here memory allocation is not necessary. The strrev function works on the original name, so all you need is
void rstring::rev()
{
 strrev(name);
}

Are you sure your instructor wants you to use strrev? Most instructors would want the student to write strrev themselves as part of the exercise. Check with your instructor.

Also, in your default constructor you should add name[0] = 0;
rstring()
{
len=0;
name=new char[len+1];
name[0] = 0;
}
because sttrev does not use len, it looks for the zero byte to know where the end of the string is.

The above corrections will fix your program. The next part is optional.

I would suggest that you not use
char a[20];
The proper way to do it is to
#include <string>

Then in main do
std::string a;
std::getline(cin, a);
rstring obj1(a.c_str());

This method uses a C++ string to get input. The c_str() method gives you access to the characters inside the C++ string. It gives you access to the char*. In this way there is no danger of the program crashing if the user puts in a name longer than 20 characters. No matter how big you make the array "a", it is always possible for the user to put in a longer name and corrupt memory.

Good luck on your assignment.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.