OK, i'm having a few problems with my program below. I want to write a reverse function and call it in main [(this is in addition to other functions i have added to mystring class) but any code i've written doesn't work properly. Using the code below it allows me to enter a string and prints it out in reverse. While this isnt an ideal solution it kind of works in that it does reverse the string. Another problem is that after reversing the string, it skips the rest of my main function ie., i want to enter another string and copy it 'n' times. if anyone can see how I can get the rest of program to be called and also how i could call reverrse as a function rather than including it in main I would appreciate any help. Thanks.
Code:
// Overloading the function print()
// and the operator + . In this program,
// operator+() is a friend function. This
// time, member functions of the class are
// defined after the class (i.e.not inline)
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int max_len = 300;
// Definition of the mystring class
class mystring
{
public:
void assign(char* t);
int length();
void print();
void print(ostream& os);
friend mystring&
operator*(const mystring& a, int n);
friend mystring
operator+(const mystring& a, const mystring& b);
friend int
operator==(const mystring &str, const mystring &str2);
char& operator[](int index);
friend istream& operator>>(istream& is, mystring& s);
friend ostream& operator<<(ostream& os, mystring& s);
private:
char s[max_len];
int len;
};
// Member functions of the mystring class
void mystring::assign(char* t)
{
strcpy(s, t);
len = strlen(s);
}
int mystring::length()
{
return (len);
}
void mystring::print()
{
cout << s << "\nLength: " << len << "\n";
}
// Prototype of a general function (not a
// member of the mystring class)
void print(char* str);
// The program’s main function
int main()
{
char str[300];
char *start, *end;
int len;
char t;
cout << "Original: ";
cin >> str;
len = strlen(str);
start = str;
end = &str[len-1];
while(start < end) {
// swap chars
t = *start;
*start = *end;
*end = t;
// advance pointers
start++;
end--;
}
cout << "Reversed: " << str << "\n";
{
int multiple;
mystring testString;
mystring copyString;
cout << "Enter string to be copied: ";
cin >> testString;
cout << "Enter number of times to copy: ";
cin >> multiple;
copyString = testString * multiple;
cout << copyString << endl;
}
}
// This is the function definition
// of the overloaded * operator.
mystring& operator*(const mystring& a, int n)
{
if (a.len * n < max_len)
{
mystring tmp;
strcpy(tmp.s, a.s);
for (int i = 1; i < n; ++i)
{
strcat(tmp.s, a.s);
}
return tmp;
}
else {
cerr << "\nERROR: Max length exceeded\n\n";
exit(1);
}
}
// This is the function definition
// of the overloaded + operator.
mystring operator+(const mystring& a, const mystring& b)
{
mystring tmp;
if (a.len + b.len < max_len)
{
strcpy(tmp.s, a.s);
strcat(tmp.s, b.s);
tmp.len = a.len + b.len;
}
else
return tmp;
}
// Definition of the general print function
void print(char* s)
{
cout << s << "\nLength: " << strlen(s) << "\n";
}
inline int operator==(const mystring &str,
const mystring &str2)
{
// recall the strcmp function returns
// 0 if strings match, but this
// function is to return non-zero
// (i.e. true) for a match
return(strcmp(str.s, str2.s) == 0);
}
char& mystring::operator[](int index)
{
if (index > len-1)
return s[len-1];
else
return s[index];
}
istream& operator>>(istream& is, mystring& str)
{
const int bufsize = 256;
char buf[bufsize];
if (is.get(buf, bufsize))
str.assign(buf);
return is;
}
ostream& operator<<(ostream& os, mystring& s)
{
s.print(os);
return os;
}
// general print function for the string
// class (for any output stream, not just
// cout as before)
void mystring::print(ostream& os)
{
os << s;
}