We've been told to design our own class for strings. I've sat down and managed everything so far but I've stumbled accorss something. We've been asked to implement a concat algo.

strings* strings::concat(strings *s)
{
	char str[10];

		for(int i=0; i<5;i++)
		{
		str[i]=m_psz[i];
		}

		for (int j = 0 ; j < 5 ; j++)
		{
		str[j+5] = strings.m_psz[j];
		}

	return s;
}

I've managed to write a main for it

s1 = new strings("hello");
	s2 = new strings("world");
	s3 = new strings("00000000000000000000000000000000000000000000000");
string *s3 = s1->concat(s2);

	s3->print();

	delete s1, s2,s3;

I know its imcompolete but its also driving me nuts.....any ideas?

Quickie concatenation algorithm.

doesnt help much since im passing it 2 objects... I've got it working but I dont know how to pass the object back:

strings* strings::concat(strings *s)
{
	char str[10] = {0};
	cout << endl;

		for(int i=0; i<10;i++)
		{
			str[i]=m_psz[i];
		}
		cout << "Value of str array is: "<<str << endl;

		for (int j = 0 ; j < 5 ; j++)
		{
			cout<< s->m_psz[j];
			str[j+4] = s->m_psz[j];
		}
		cout << "After function the str value is: "<< str;

	return str;
}

only when I put return str it errors and moans at me.

s1 = new strings("hello");
	s2 = new strings("world");
	s3 = new strings("00000000000000000000000000000000000000000000000");

	
	s1->print();
	s2->print();
	
	

cout << "\n**Now calling the concat function **\n" <<endl;

s3 = s1->concat(s2);

	s3->print();

Concatenation to me means adding text to an existing string. Apparently your version is to take two strings and create a new third string. But you aren't new ing the third string when you create it (from the two strings that will compose it).

Concatenation to me means adding text to an existing string. Apparently your version is to take two strings and create a new third string. But you aren't new ing the third string when you create it (from the two strings that will compose it).

well I've had a go at putting the new in :

s3 = new strings(NULL) ;
s3 = s1->concat(s2);

	s3->print();

delete s1, s2;

the question is:


...a useful method function could be the following one:

class Strings {

private :
..
..

public :
..
strings *concat (string *s);

};

The prupose of the concat method is to take the current object (on which the method is called) as well as the passed strings object and produce a new string object which consitist of joining those 2. An illustrating texting code for the method might look like:

strings *s1 = new string("Hello);
string *s2 = new string("World");

string s3 = s1->concat(s2);
s3->print();

delete s1, s2 ,s3;

the above code should print out HelloWorld

Ah. Here was my messing with a setup.

#include <iostream>
#include <cstring>
#include <cstddef>

namespace my
{
   class string
   {
      char *text;
   public:
      string(const char *init = "")
      {
         std::size_t size = strlen(init);
         text = new char[size + 1];
         strcpy(text, init);
      }
      ~string()
      {
         delete[] text;
      }
      string &concat(const string &s)
      {
         std::size_t size = strlen(text) + strlen(s.text);
         char *temp = new char[size + 1];
         strcpy(temp, text);
         delete[] text;
         strcat(temp, s.text);
         text = temp;
      }
      friend std::ostream& operator<< (std::ostream &o, string &s);
   };

   std::ostream& operator<< (std::ostream &o, my::string &s)
   {
      return o << s.text;
   }
}

int main()
{
   my::string one("Hello"), two(" world");
   std::cout << one << '\n';
   std::cout << two << '\n';
   one.concat(two);
   std::cout << one << '\n';
   return 0;
}

/* my output
Hello
 world
Hello world
*/

There should be some of the general ideas you are looking for in there.

[edit]Some mods:

string *concat(const string &s)
      {
         std::size_t size = strlen(text) + strlen(s.text);
         char *result = new char[size + 1];
         strcpy(result, text);
         strcat(result, s.text);
         return new string(result);
      }
int main()
{
   my::string one("Hello"), two(" world"), *three = one.concat(two);
   std::cout << one << '\n';
   std::cout << two << '\n';
   std::cout << *three << '\n';
   return 0;
}

But my C++ sucks in this area, so I'm pretty sure it's missing correctness.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.