hi,
the following program has got to take a user inputted string and allows the user to see the postion of each character in the array i.e.
T H I S I S A T E S T
0 1 2 3 4 5 6 7 8 9 10 11 12
then it ask the user to search for any of the characters and it gives them their first and last position i.e for T 1st =0 last = 12
then it has to reverse the string. At first I had this working in one class, but I now need to split it into seperate cpp and header files.
but now i'm getting errors that i'm have problems fixing, probably something simiple that i just can't see.
Here are the 3 seperate files:
MyString.cpp
#include "MyString.h"
#include <string>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::setw;
/**
MyString constructor, we set the str pointer to NULL to make sure it is initialised
properly, then call the set() method to avoid code duplication.
*/
MyString::MyString(const char* inputString /* = NULL */) : str(NULL)
{
set(inputString);
}
/**
Destructor, needs to deallocate all memory allocated to the object, in this case
this means any memory that's allocated to store the string
*/
MyString::~MyString()
{
// if the str pointer is not NULL, we need to deallocate the memory for the string
// stored
if (str != NULL)
{
delete[] str;
}
}
void MyString::set(const char* inputString)
{
// Get amount mem that needs allocating from length of input string
int inputLength = strlen(inputString);
// Allocate memory for holding string, with 1 extra for null terminator
str = new char[inputLength + 1];
// Copy contents from input string to memory allocated
strcpy (str , inputString);
}
void MyString::getLength(char Array[])
{
int i = 0; // declaring new int 'i' with a value of 0
do // do this
{
i++; // increment 'i'
}
while (Array[i] != '\0');
return i;
}
void MyString::printFormatted(char Formatted[])
{
for (int format = 0; format < getLength(Formatted); ++format)
{
cout << setw(4) << Formatted[format];
}
cout << endl;
for (int formatP = 0; formatP < getLength(Formatted); ++formatP)
{
cout << setw(4) << formatP;
}
cout << endl;
}
void MyString::findFirst(char First[], char c)
{
for (int findF = 0; findF < getLength(First); --findF)
{
if (First[findF] == c)
{
return findF;
}
}
return -1;
}
void MyString::findLast(char Last[], char c)
{
for (int findL = getLength(Last); findL > 0; --findL)
{
if (Last[findL] == c)
{
return findL;
}
}
return -1;
}
void MyString::reverse()
{
for (int i = str1.length()-1; i>=0; i--)
{
cout <<str1 [i];
}
}
ostream& operator<<(ostream& output, const MyString& mstr)
{
// can access the private member string because the function is a
// friend of MyString.
if (mstr.str != NULL)
{
output << mstr.str;
}
return output;
}
/**
Overloaded input stream operator
A non-constant reference is used as the MyString object is being
modified.
*/
istream& operator>>(istream& input, MyString& mstr)
{
char strBuffer[MY_STRING_BUFFER_SIZE];
input >> strBuffer;
mstr.set(strBuffer);
return input;
}
MyString.h
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
// Allow MyString to use input/output streams by name
using std::ostream;
using std::istream;
using std::cout;
using std::endl;
// Used for character array buffers for reading
const int MY_STRING_BUFFER_SIZE = 256;
class MyString
{
private:
char* str;
public:
MyString(const char* inputString = NULL);
/**
This is the destructor, and deallocates any memory that
has been explicitly allocated to this class.
*/
~MyString();
void set(const char* inputString);
int getLength(/*char Array[]*/);
void printFormatted(/*char Formatted[]*/);
int findFirst(char c);
int findLast(char c);
void reverse();
friend ostream& operator<<(ostream& output, const MyString& str);
friend istream& operator>>(istream& input, MyString& str);
};
#endif // MYSTRING_H
Main.cpp
#include <iostream>
#include "MyString.h"
using std::cin;
using std::cout;
using std::endl;
int main()
{
// Create an empty MyString object
MyString mstr;
// Create a buffer so we can read input
char strBuffer[MY_STRING_BUFFER_SIZE];
// Read a line...
cout << "Please input a string: ";
cin.getline(strBuffer, MY_STRING_BUFFER_SIZE);
// And put it into the MyString object
mstr.set(strBuffer);
// Print formatted...
cout << "You input:" << endl;
mstr.printFormatted();
// Length
cout << "Length of string: " << mstr.getLength() << endl;
// Read character, and search
char c;
cout << "Please enter a character to search for: ";
cin >> c;
cout << "First occurrence of '" << c << "' at: " << mstr.findFirst(c) << endl;
cout << "Last occurrence of '" << c << "' at: " << mstr.findLast(c) << endl;
// Reverse
mstr.reverse();
cout << "Reverse string: " << mstr << endl;
return 0;
}