Dynamic char array length

I'm very new to C++ programming and I'm having a little trouble with a function I'm making. It's supposed to take a char array containing a sentence and output the first word.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
char* fSplit(char* fString)
{	
	int fWordLen(0);
	if(fString != " ")
	{
		for(int i = 0; i < static_cast<int>(strlen(fString)); i++)
		{
			if(fString[i] == ' ')
			{
				fWordLen = i+1;
				break;
			}
		}	
		char* fWord = new char[fWordLen];
		for(int x = 0; x < (fWordLen-1); x++)
			fWord[x] = fString[x];
		fWord[fWordLen] = '\n';
		return fWord;
	}
}


In line 14, it allocates the amount of space needed to hold the word plus the null terminating character, but for some reason it's allocating too much space so garbage characters are at the end of the word. I've tried putting in constants (e.g. 1, 3, 5) but it always seems to be allocating the same amount of space instead of the amount I'm asking for. What am I doing wrong?
C Strings are null terminated.

You're not allocating enough space for the text and the null. And you're not placing the null at the end of the string. You need these changes:
char* fWord = new char[fWordLen + 1];
...
fWord[fWordLen] = '\0';

You also need to return something if you have leading spaces, null probably.
Last edited on
Oh right, I was using '/n' instead of '/0'. Thanks, that fixed it.
What am I doing wrong?

Using arrays of char to represent strings. That's not a useful exercise.

C++ has actual strings:

1
2
3
4
5
6
7
8
string fSplit(const string& fString)
{
    size_t pos = fString.find(' '); // find first space
    if(pos == string::npos)         // if no spaces were found
         return fString;            // return the whole string
    else                            // otherwise
         return fString.substr(0, pos);  // return the sub-string
}

demo: http://ideone.com/eKX4W

C++ also has ways to process individual words in a string

1
2
3
4
5
6
string fSplit(const string& fString)
{
    string word;
    istringstream(fString) >> word;
    return word;
}

demo: http://ideone.com/XyWOV
old compiler demo: http://ideone.com/69sDe

(the last example also skips leading whitespace. if you want it to behave like the first and like yours was intended, do a >> noskipws >> word;)
Thanks for the info. I'm reading Ivor Horton's Beginning Visual C++ 2010 and it hasn't made any mention of strings like that yet.
there are many bad C++ books out there. And we are not talking about bad style, but things like sporting glaringly obvious factual errors and promoting abysmally bad programming styles
-- http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
closed account (4oL1hbRD)
I'm just curious, but would you put a delete statement in this code somewhere to prevent memory loss?

1
2
delete fword;
fword = NULL;


If so, where would you put it? Could you put it in the function after doing a hard copy of fword and return the copy instead?

Or is it even necessary or is there a better way.

Topic archived. No new replies allowed.