How can I add a character to the end of a string?

  • Thread starter Thread starter pazmush
  • Start date Start date
  • Tags Tags
    Class String
Click For Summary

Discussion Overview

The discussion revolves around the implementation of a custom string class in C++, specifically focusing on adding characters to the end and beginning of a string. Participants explore operator overloading for the '+' operator and seek advice on how to handle character concatenation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares their implementation of a custom string class and expresses difficulty in adding characters to the end and beginning of the string.
  • Another participant reports success in modifying the code to add a character to the end but seeks advice on how to prepend a character.
  • Concerns are raised about the complexity of operator overloading, particularly for the '+' operator when dealing with different types (string vs. char).
  • A suggestion is made to use the syntax string('f') + a to achieve the desired concatenation.
  • One participant notes that while the suggested method works, they prefer to add characters directly without creating a new string.
  • Another participant explains that the inability to use char + string arises because char does not have member functions, unlike string, which has an operator+ member function.
  • A suggestion is made to use the string.insert() function for prepending a character without creating a new string.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to adding characters to strings, with some agreeing on the limitations of operator overloading while others propose alternative methods. The discussion remains unresolved regarding the most efficient way to achieve the desired functionality.

Contextual Notes

Participants highlight the complexity of operator overloading and the limitations of using built-in types like char in conjunction with user-defined types like string. There are also mentions of potential pitfalls in memory management within the custom string implementation.

pazmush
Messages
32
Reaction score
0
hi, this is what I've got so far and what I'm wanting to do is add a char to the end(and eventually the beginning) of the string but i can't seem to get it to work, do you think I've got the right idea

Code:
#include "string.h"
#include <iostream>

using namespace std;
using namespace PC3762;
string::string(char* instring)
{
	_N=strlen(instring);

	if (_N > 0) 
	{
		_a = new char [_N+1];

		for (size_t i = 0; i < _N; ++i )
			_a[i] = instring[i];	

		_a[_N] = 0;
	}
}string::string (const string& rca)
{
	_a = NULL;
	_N = rca._N;

	if (_N > 0) 
	{
		_a = new char [_N+1];

		for (size_t i = 0; i < _N; ++i ) 
			_a[i] = rca._a[i];
		_a[_N]=0;
	}
		
}
string string::operator= (const string& rhs) 
{

  if (&rhs == this) return *this;  // Always check this for assignment.
  
  _N = rhs._N;

  delete _a;  // Delete any existing array - OK even if _a == 0.

  if (_N > 0) 
  {
	  _a = new char [_N+1];
	  
	  for (size_t i = 0; i < _N; ++i )
		  _a[i] = rhs._a[i];
	  _a[_N]=0;
  }
	
  return (*this);
}
std::ostream& PC3762::operator<< (std::ostream& os, const string& rca) 
{
	os << "tring" << rca._N << ":";
	for (size_t i = 0; i < rca._N; ++i )
		os << ' ' << rca._a[i];
	return os;
}string::~string() 
{
	delete _a;						// OK even if _a == 0.
}

string string::operator+ (string rhs) 
{

	string temp("hi");

	delete temp._a;
   
	temp._a = _a;

	int l=_N;

   _N = _N+rhs._N;

  if (_N > 0)
  {
	  _a = new char [_N+1];

	  for (size_t i = 0; i < l; ++i )
		  _a[i] = temp._a[i];
	  for (size_t i = 0; i < rhs._N; ++i)
		  _a[l+i] = rhs._a[i]; 
  }

  return *this;

}

string string::operator+ (char rhs) 
{
	string temp("hi");
	delete temp._a;
	temp._a = _a;

	int l = _N;

  if (_N > 0)
  {
	  _a = new char [_N+1];

	  for (size_t i = 0; i < l; ++i )
		  _a[i] = temp._a[i];

	  _a[_N+1] = rhs;
	  
	  
  }

  return *this;

}

thanks
 
Last edited:
Physics news on Phys.org
got the first bit working, had to change _a[_N+1] to _a[_N-1]

has anyone any advice on where to start with adding from the other side

i.e.
at the moment i can add

string a("hello"), b;

b = a+'f';

but i can't do

b = 'f'+a

?
 
Last edited by a moderator:
I suggest saying string('f')+a.
 
yeah, that does work, but i was hoping to be able to just add characters on there own
 
pazmush said:
yeah, that does work, but i was hoping to be able to just add characters on there own
I suggest reading mgb_phys' link-- if you say "a + b" and one of these is a class, you are actually generally calling a member function on "a" called operator+. "string" has such a member function. "char" does not have any member functions. Therefore you can say (string + char) or (string + string) but you cannot say (char + string).

This aside if you really just want to prepend a character to a string without creating a new string, you can just use the string.insert() function.
 

Similar threads

  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
89
Views
7K
Replies
55
Views
7K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K