- Engineering
- Computer Science
- i am having problem getting the right output i have...
Question: i am having problem getting the right output i have...
Question details
I am having problem getting the right output, I have provided the output needed for the program, and the output I am having. Please don't make changes to text.h file, only text.cpp
Sample output
My output
text.h
//--------------------------------------------------------------------
//
// Laboratory 1 Text.h
// **Instructor's Solution**
// Class declaration for the array implementation of the Text
ADT
//
//--------------------------------------------------------------------
#ifndef TEXT_H
#define TEXT_H
#include <stdexcept>
#include <iostream>
using namespace std;
class Text
{
public:
// Constructors and operator=
Text(const char *charSeq = ""); // Initialize using
char*
Text(const Text &other); // Copy constructor
void operator = (const Text &other); //
Assignment
// Destructor
~Text();
// Text operations
int getLength() const; // # characters
char operator [] (int n) const; // Subscript
void clear(); // Clear string
// Output the string structure -- used in
testing/debugging
void showStructure() const;
//--------------------------------------------------------------------
// In-lab operations
// toUpper/toLower operations (Programming Exercise
2)
Text toUpper() const; // Create upper-case copy
Text toLower() const; // Create lower-case copy
// Relational operations (Programming Exercise
3)
bool operator == (const Text& other) const;
bool operator < (const Text& other)
const;
bool operator > (const Text& other) const;
private:
// Data members
int bufferSize; // Size of the string buffer
char *buffer; // Text buffer containing a
null-terminated sequence of characters
// Friends
// Text input/output operations (In-lab Exercise
1)
friend istream & operator >> (istream&
input, Text& inputText);
friend ostream & operator << (ostream&
output, const Text& outputText);
};
#endif
text.cpp
#include "pch.h"
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cstring>
#include "Text.h"
Text::Text(const char *charSeq)
{
bufferSize = strlen(charSeq) + 1;
buffer = new char[bufferSize];
strcpy(buffer, charSeq);
}
Text::Text(const Text &other)
{
bufferSize = other.bufferSize;
buffer = new char[bufferSize];
strcpy(buffer, other.buffer);
}
void Text::operator = (const Text &other)
{
int len = other.bufferSize;
if (len > bufferSize)
{
delete[] buffer;
bufferSize =
other.bufferSize;
buffer = new
char[bufferSize];
}
strcpy(buffer, other.buffer);
}
Text::~Text()
{
delete[] buffer;
}
int Text::getLength() const
{
return bufferSize - 1;
}
char Text::operator [] (int n) const
{
// check if n is out of range first
if (n >= 0 && n < getLength()) {
return buffer[n];
}
return 0;
}
void Text::clear()
{
buffer[0] = '';
}
void Text::showStructure() const
// Outputs the characters in a string. This operation is intended
for
// testing/debugging purposes only.
{
int j; // Loop counter
for (j = 0; j < bufferSize; j++)
cout << j;
cout << endl;
for (j = 0; buffer[j] != ''; j++)
cout << buffer[j] <<
endl;
}
Text Text::toUpper() const
{
Text temp(buffer);
for (int i = 0; i < bufferSize; i++)
{
if ((buffer[i] > 96 &&
(buffer[i] < 123)))
{
temp.buffer[i] =
char(buffer[i] - 32);
}
}
return temp;
}
Text Text::toLower() const
{
Text temp(buffer);
for (int i = 0; i < bufferSize; i++)
{
if ((buffer[i] > 64 &&
(buffer[i] < 91)))
{
temp.buffer[i] =
char(buffer[i] + 32);
}
}
return temp;
}
bool Text::operator == (const Text& other) const
{
{
if (getLength() ==
other.getLength())
{
if (bufferSize
== 0)
{
return true;
}
else
{
for (int i = 0; i < bufferSize; i++)
{
if (buffer[i] !=
other.buffer[i])
{
return
false;
}
}
return true;
}
}
else
{
return
false;
}
}
}
bool Text::operator < (const Text& other) const
{
if (getLength() < other.getLength())
{
return true;
}
else
{
return false;
}
}
bool Text::operator > (const Text& other) const
{
if (getLength() > other.getLength())
{
return true;
}
else
{
return false;
}
}
Solution by an expert tutor
