Algorithm to Split a String into an Array by a character

Hello.

I'm trying to split my GString (my own string class) into a GArray (my own vector class) by a character called Splitter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// THE SPLIT FUNCTION
static GArray<GString>& Split(const char Splitter, const GString& str)
{
	GArray<GString>* result = new GArray<GString>(str.Size());

	int splitterPos = 0;
        int currentPos = 0;
	int previousPos = 0;

	do
	{
		splitterPos = str.Find(Splitter, currentPos);					

		if (splitterPos >= 0)
		{
			currentPos = splitterPos;
			result->Add(str.SubString(previousPos, currentPos - 1));
			currentPos++;
			previousPos = currentPos;
		}

	} while (splitterPos >= 0);

	return *result;				
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// THE GArray CONSTRUCTOR
template<typename GType>
class GArray
{
	GType* array_type = nullptr;
	int size = 0;

public:
	explicit GArray(int _Size)
	{
		size = _Size;
		array_type = new GType[size]();
	}

// THE GArray ADD function
void Add(GType Item)
{
	size++;

	GType* temp = new GType[size];

	for (int i = 0; i < size - 1; i++)
	{
		temp[i] = array_type[i];
	}

	temp[size - 1] = Item;

	delete[] array_type;
	array_type = temp;
	temp = nullptr;
}


I can assure you the GString::Find() and GString::SubString() functions work perfectly.

This is the output I get: http://prntscr.com/9xswkz

I don't know where's the error, but I'm sure it's the algorithm (the same algorithm implemented with std::string and std::vector<> didn't work...!!!)

Any tip?
Last edited on
When you create a GArray with that constructor, you create with some number of objects.

So, if str consists of 32 characters, you intialize result to point to a GArray<GString> object with 32 Gstrings already in it. When you Add to your GArray object, you add more GString objects.

Also, your Split function should probably return a GArray by value.
@cire thanks for the answer

Well, as a size for the GArray you're right: I was exaggerating.

I'm now assuming the size of the GArray in this way: in the array there will be as many strings as the number of splitters + 1

Example: "my.favourite.string" [ 2 splitters + 1 = 3 strings = my / favourite / strings]
Example: "best.programmer" [1 splitter + 1 = 2 strings = best / programmer]

Obviously if we have a string like "...hello.bye", I'll first remove the initial dots before splitting.

Anyway, as a test with "my.favourite.string" I put 3 as size of the GArray in the Split function

But now I only get less spaces than before, but the problem persists:

screen: http://prntscr.com/9xtdkw
I'm now assuming the size of the GArray in this way: in the array there will be as many strings as the number of splitters + 1

I take it you made no changes to GArray type? If not, and you create a GArray of size 4, it begins with 4 GString objects in it. And, if you Add other objects to it, those will be in addition to those already contained by the GArray object.
Last edited on
You're right. I set 0 now. Since it's a dynamic array, there'll be no problem to Add() values to it.

Anyway, this is the output: http://prntscr.com/9xtuqm

The issue is still there <.<
Topic archived. No new replies allowed.