My goal is to take a string and do various modifications with it like reversing it, printing it, printing it up to n places, and comparing it to another string using the class my_string.
This is what I have so far
Code:
#include <iostream>
#include <string>
using namespace std;
class my_string {
public:
my_string() : len(0)
{ s = new char[1];assert(s != 0); s[0] = 0; }
my_string(const my_string& str); //copy constructor
my_string(const char* p); //conversion constructor
~my_string() { delete []s; }
int strcmp(const my_string& a) const;
void print(int n) const; //int-overloaded print
void print() const { cout << s; }
void strrev() const;
private:
char* s;
int len;
};
This is the class. There's the default constructor, the copy constructor and the conversion constructor, and the destructor. It's just supposed to take a string, a pointer to char. I don't think there's anything wrong with it (But I dunno).
Code:
void my_string::print(int n) const
{
if (n < 0 || n > len)
cout << "error";
else
for (int i = 0; i < n; i++)
cout << s[i];
cout << endl;
}
This function is for taking a string to be printed up to n places. I haven't had any problems with it.
Code:
int my_string::strcmp(const my_string& a) const
{
int i;
for (i = 0; s[i] && a.s[i] && (s[i] != a.s[i]); ++i)
;
return (s[i] - a.s[i]);
}
This is my first problem. My goal is to send two strings to this function and it returns a 0 if the strings are lexicographically equal, a positive number if the first string (s) is greater, and a negative number if the second string (a.s) is greater. For some reason I'm just never getting the right return numbers. It will say two strings are equal when they're not, (It says Gladiator and Gladiolus are equal)
Code:
void my_string::strrev() const
{
int i;
char* temp;
temp = new char[len + 1];
for(i = 0; i < (len); i++)
temp[i] = len - i;
temp[i] = '\0';
strcpy(s, temp);
delete[] temp;
}
This is my next problem. My goal with this function is to reverse the string passed to my_string. For some reason, I keep getting garbage. I can't figure out why.
Code:
my_string::my_string(const char* p)
{
len = strlen(p);
s = new char[len + 1];
assert(s !=0);
strcpy(s, p);
}
my_string::my_string(const my_string& str) : len(str.len)
{
s = new char[len + 1];
assert(s != 0);
strcpy(s, str.s);
}
Just the copy and conversion constructors.
Code:
int main()
{
my_string s1( "gladiator" );
my_string s2( "gladiolus" );
my_string s3( "xyz" );
my_string s4( "abcd" );
my_string s5( "same" );
my_string s6( "same" );
s1.print(); //print the string as is
cout << endl;
int return_value;
return_value = s1.strcmp(s2); //
if (return_value == 0){
s1.print();
cout << " is lexicographically equal to ";
s2.print();
cout << "\n" << endl;
}
else if (return_value < 0)
cout << "String 1 is lexicographically less than String 2\n" << endl;
else
cout << "String 1 is lexicographically greater than String 2\n" << endl;
s1.print(5); //print the string up to the Nth place
cout << endl;
s1.strrev(); //reverse the string
s1.print();
system("pause");
}
Just the main function.
I can't see what's wrong. I don't know if my syntax or if my logic is off. Can someone point out something I don't see?