By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,619 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

ADT string

P: 4
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(Newstring)
// 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(char)
// concatenates a single char onto the end of the private data

substring(Newstring)
// 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(title);
title.display();
title.concatenate(" save me!");
title.display();

lesson.length();
lesson.display();
lesson.concatenate("s");
lesson.display();
bool substring();
}


Newstring.h
#ifndef TEST_H
#define TEST_H

class Newstring
{
public:
Newstring (char *); //constructor
int length(void);
void concatenate(char *);
void concatenate(char);
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::Newstring(char *str)
{
strcpy_s(data, str);
}

void Newstring::length(void)
{
int len = 0;
int i = 0;

while (data[i] != NULL)
{
len++;
i++;
}
cout << "The length of the Newstring is " << length <<endl;
}

void Newstring::concatenate (char *str)
{
strcat(data, str);
}

void Newstring::display(void)
{
cout << data <<endl;
length(data);
}
Jun 5 '07 #1
Share this Question
Share on Google+
7 Replies


Savage
Expert 100+
P: 1,764
I don't see data declaration,what is it?

A array of chars?

Savage
Jun 5 '07 #2

P: 4
I don't see data declaration,what is it?

A array of chars?

Savage
The data declaration is char data[256] listed in private section of the class Newstring located in the file Newstring.h. It takes the information from the string title and string lesson listed in the file Newstring.cpp. All file names are bolded.

I remove all parts of length and substring function so you can copy the program and run it.

The output of the program so far is:

The wages of sins is death
The wages of sins is death save me
The gift of god is eternal life
The gift of god is eternal lifes

I need help with adding in substring(Newstring) and length. I am having problem completing those. The requirement for these are listed in the first submittion of the problem.

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.display();
title.concatenate(" save me!");
title.display();

lesson.display();
lesson.concatenate("s");
lesson.display();
}


Newstring.h
#ifndef TEST_H
#define TEST_H

class Newstring
{
public:
Newstring (char *); //constructor
void concatenate(char *);
void concatenate(char);
void display(void);
private:
char data[256];
};
#endif


Newstring1.cpp
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include "Test.h"

using std::endl;
using std::cout;

Newstring::Newstring(char *str)
{
strcpy_s(data, str);
}

void Newstring::concatenate (char *str)
{
strcat(data, str);
}

void Newstring::display(void)
{
cout << data <<endl;
length(data);
}
Jun 6 '07 #3

ilikepython
Expert 100+
P: 844
The data declaration is char data[256] listed in private section of the class Newstring located in the file Newstring.h. It takes the information from the string title and string lesson listed in the file Newstring.cpp. All file names are bolded.

I remove all parts of length and substring function so you can copy the program and run it.

The output of the program so far is:

The wages of sins is death
The wages of sins is death save me
The gift of god is eternal life
The gift of god is eternal lifes

I need help with adding in substring(Newstring) and length. I am having problem completing those. The requirement for these are listed in the first submittion of the problem.

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.display();
title.concatenate(" save me!");
title.display();

lesson.display();
lesson.concatenate("s");
lesson.display();
}


Newstring.h
#ifndef TEST_H
#define TEST_H

class Newstring
{
public:
Newstring (char *); //constructor
void concatenate(char *);
void concatenate(char);
void display(void);
private:
char data[256];
};
#endif


Newstring1.cpp
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include "Test.h"

using std::endl;
using std::cout;

Newstring::Newstring(char *str)
{
strcpy_s(data, str);
}

void Newstring::concatenate (char *str)
{
strcat(data, str);
}

void Newstring::display(void)
{
cout << data <<endl;
length(data);
}
For the lenght function try changing the if statement to:
Expand|Select|Wrap|Line Numbers
  1. if (data[i] != '\0')
  2.  
Jun 6 '07 #4

P: 4
For the lenght function try changing the if statement to:
Expand|Select|Wrap|Line Numbers
  1. if (data[i] != '\0')
  2.  
Thanks for the advice but it didn't work. I got the following errors:

1>c:\Newstring.h(15) : error C2365: 'Newstring::length' : redefinition; previous definition was 'member function'
1> c:\Newstring.h(8) : see declaration of 'Newstring::length'


1>c:\Newstring.h(15) : error C2365: 'Newstring::length' : redefinition; previous definition was 'member function'
1> c:\Newstring.h(8) : see declaration of 'Newstring::length'


1>c:\Newstring1.cpp(25) : error C3867: 'Newstring::length': function call missing argument list; use '&Newstring::length' to create a pointer to member
1>Generating Code...

1>Newstring - 3 error(s), 0 warning(s)
Jun 6 '07 #5

ilikepython
Expert 100+
P: 844
Thanks for the advice but it didn't work. I got the following errors:

1>c:\Newstring.h(15) : error C2365: 'Newstring::length' : redefinition; previous definition was 'member function'
1> c:\Newstring.h(8) : see declaration of 'Newstring::length'


1>c:\Newstring.h(15) : error C2365: 'Newstring::length' : redefinition; previous definition was 'member function'
1> c:\Newstring.h(8) : see declaration of 'Newstring::length'


1>c:\Newstring1.cpp(25) : error C3867: 'Newstring::length': function call missing argument list; use '&Newstring::length' to create a pointer to member
1>Generating Code...

1>Newstring - 3 error(s), 0 warning(s)
You have a function and an integer both named "lenght". Try changing the name of the integer to something else.
Jun 6 '07 #6

P: 4
You have a function and an integer both named "lenght". Try changing the name of the integer to something else.
I tried it--it didn't work
Jun 7 '07 #7

ilikepython
Expert 100+
P: 844
I tried it--it didn't work
You got the exact same errors?
Jun 7 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.