Getting Substring from class string object

Mar 12, 2017 at 6:01am
Is there a problem in my class getSubString Fuction.
there are two arguments taken (starting-index and length(that how big you want your substring to be))

"this is interesting." is calling object.
the main program takes the argument(start-index = 8, length = 11)

1
2
3
4
5
6
7
8
9
10
11
12
13
	int size = this->getLength() - startIndex;
	if(this->length == length)
		size = 1;
	int x = size <= length? size : length;
	char* temp = new char [x];
	int j = startIndex;
	for(int i=0; i<x; i++)
	{
		temp[i] = this->buffer[j];
		j++;
	}
	CMPT135_String ans(temp);
	return ans;


in my output it comes


interesting%%%%%%%%%%%%%%%%%%%%%%%%%%


** %% is represted as garbage.
I may be unclear with my question. Please ask
Mar 12, 2017 at 7:16pm
Hello NabeelUsmani,

Your bit of code looks like a function, but is it a stand alone function or part of a class? The function definition would be useful to see how he function parameters are used. Some indication of how the function is called would be helpful.

I really do not want to guess at the missing parts and get it wrong.

Andy
Mar 12, 2017 at 9:41pm
Hi Andy,

First there is a Class object made like s1 = "This is interesting ." then the function is called like s1.getSubString(8,11). The function above is a function of a class.

Thanks for your reply.
Mar 12, 2017 at 10:15pm
Hello NabeelUsmani,

What is "CMPT135_String" and how is it defined and where?

Trying to put together something I can test, but having trouble trying to figure out what is in your class.

Andy
Last edited on Mar 12, 2017 at 11:30pm
Mar 12, 2017 at 11:32pm
Hello NabeelUsmani,

Got it to work, but not sure if its right or what you had in mind.

Andy
Topic archived. No new replies allowed.