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

In summary: For example, if you want to prepend the letter "a" to the string "hello", you would call string.insert('a');
  • #1
pazmush
32
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
  • #2
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

?
 
  • #3
You are doing this as an excercise right? You aren't actually planning to invent your own string class.

Operator overloading is a lot more complex than it looks, especialy for '+' when each side can be an object. Take a look at http://www.parashift.com/c++-faq-lite/operator-overloading.html
 
Last edited by a moderator:
  • #4
I suggest saying string('f')+a.
 
  • #5
yeah, that does work, but i was hoping to be able to just add characters on there own
 
  • #6
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.
 

1. What is the purpose of adding char to the String class?

The purpose of adding char to the String class is to allow for more efficient and versatile manipulation of strings. By adding char, developers can easily access and modify individual characters within a string, rather than having to manipulate the entire string as a whole.

2. Is adding char to the String class a new feature?

No, adding char to the String class is not a new feature. It has been a part of many programming languages for a long time, including Java and C++. However, some newer languages may not have this capability.

3. Can char be added to any string?

Yes, char can be added to any string, as long as the programming language supports it. However, it is important to note that the process of adding char may vary slightly depending on the language and its specific syntax.

4. How does adding char improve string manipulation?

Adding char to the String class improves string manipulation by allowing for more precise and efficient changes to be made. Rather than having to manipulate the entire string, developers can now focus on individual characters, making it easier to perform tasks such as searching, replacing, or removing specific characters.

5. Are there any potential drawbacks to adding char to the String class?

One potential drawback to adding char to the String class is that it may add complexity for beginner programmers. Additionally, if not used properly, it could potentially lead to errors or bugs in the code. It is important for developers to fully understand how to use this feature in order to avoid any potential issues.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
932
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
872
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Programming and Computer Science
4
Replies
118
Views
6K
  • Programming and Computer Science
Replies
18
Views
2K
Back
Top