The assignment was as follows:
A character string can be implemented as a linked list of characters. Implement a C++ ADT called Newstring that uses linked lists to implement the following string operations:
display()
// display private data on standard output
length()
// returns the length of string
concatenate(New string)
// copies contents of parameter Newstring onto the end of private
// data; does not merely make the last node of the private data
// point to the first node of Newstring object
concatenate(cha r)
// concatenates a single char onto the end of the private data
substring(Newst ring)
// returns true if Newstring object is a substring of the private
// data of the ADT and false otherwise
In all cases string refers to the private data of your ADT. In implementing this ADT, you may want to write additional, private methods to assist the others. Write a short program to test your ADT. You should have a minimum of three files for this project: a header file for the ADT, an implementation file for the ADT, and a file that uses the ADT.
I am having problems getting the length and substring function to work. Please help. The name of each file is listed above each file. The following is what I have done so far:
Newstring.cpp
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include "Newstring. h"
using std::endl;
using std::cout;
void main(void)
{
Newstring title("The wages of sins is death");
Newstring lesson ("The gift of god is eternal life");
title.length(ti tle);
title.display() ;
title.concatena te(" save me!");
title.display() ;
lesson.length() ;
lesson.display( );
lesson.concaten ate("s");
lesson.display( );
bool substring();
}
Newstring.h
#ifndef TEST_H
#define TEST_H
class Newstring
{
public:
Newstring (char *); //constructor
int length(void);
void concatenate(cha r *);
void concatenate(cha r);
void display(void);
bool substring (char);
private:
char data[256];
int length;
};
#endif
Newstring1.cpp
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include "Test.h"
using std::endl;
using std::cout;
Newstring::News tring(char *str)
{
strcpy_s(data, str);
}
void Newstring::leng th(void)
{
int len = 0;
int i = 0;
while (data[i] != NULL)
{
len++;
i++;
}
cout << "The length of the Newstring is " << length <<endl;
}
void Newstring::conc atenate (char *str)
{
strcat(data, str);
}
void Newstring::disp lay(void)
{
cout << data <<endl;
length(data);
}