Hi everybody,

I am trying to make the real substr function in c++ which its a member of String Class.
Here what I have done:

void substr(char x[], int n, int n2){
    char *p;
    p = x;

    for(int i=0; i<=n2; i++)
        *(p+i) = *(p+i+n);
    
	*(p+n2) = '\0';
    // I can use this
	cout << x << endl;
}

An the Main is:

int main(){
	char x[] = "It should work.";
	substr(x,3,6);

	return 0;
}

The problem is that I can't return the array of char[]. BTW, I don't want to use anything use <string> library.

can any one figure the function to return char[] so I can use this in main:

char y[] = substr(x,3,5)

Regards,

Recommended Answers

All 5 Replies

As it is now, the original string gets replaced by the sub-string, so you do get the sub-string back from the function. But if you want it like you said. First you need to allocate new memory for the sub-string, with the right size (n2 + 1). Then you write into that memory exactly the way you are doing now, and you give the pointer back as output. But,
1. you need to allocate memory for the sub-string with "malloc()" (if you are against <string>, I presume you are programming in C, otherwise use "new")
2. the string that is outputted needs to get deleted, and it needs to correspond to how it got allocated, so if you use "malloc" then delete with "free()" (if you use "new" then delete with "delete").

The above second point is the reason why, generally, people don't program functions that return pointers to newly allocated memory, it's not safe to assume the caller will know which deleter to use (in case of hybrid C/C++ code). So usually, you add a parameter to the function which points to a place with enough memory to store the sub-string.

Finally, save yourself the trouble and just use "memmove()"!

Actually, I hope that I can understand all what you had just said, but I am just a really small noob. Also, I am using c++ not c, and sure my way is not good at all, but this what I came with. I will be gratefully if you could provide me with better ideas/codes I can use.

I am ready to change the whole function if I can do it right with other.
What I really trying to do is to use arrays of char[] instead of strings and applying string's functions.
Also. How can I use and benefit from "new"?

Thank you.

Well this is one way that is with your function, which is totally fine btw, there aren't that many ways to write a sub-string after all:

//notice the return type here.
char* substr(char x[], int n, int n2){
    char *p = new char[n2+1]; //This allocates the memory to hold the sub-string.

    //the rest is essentially the same
    for(int i=0; i<=n2; i++)
        *(p+i) = *(x+i+n); //here x instead of p on the rhs.
   
    *(p+n2) = '\0';
    // I can use this
    cout << p << endl; //p instead of x.
    return p;
}

int main(){
	char x[] = "It should work.";
	char* p = substr(x,3,6);
        ... do something with p ...
        delete[] p; //delete it again
	return 0;
}

But this is a simple way, without using <string>:

int main(){
	char x[] = "It should work.";
	char* p = new char[7];
        memmove(p,&x[3],6); p[6] = '\0';
        ... do something with p ...
        delete[] p; //delete it again
	return 0;
}

Or even better:

#define MY_SUBSTR(RETURN_POINTER,STRING_POINTER,START_INDEX,SUBSTR_SIZE) = new char[SUBSTR_SIZE + 1];\
memmove(RETURN_POINTER,&STRING_POINTER[START_INDEX],SUBSTR_SIZE); RETURN_POINTER[SUBSTR_SIZE] = '\0';
int main(){
	char x[] = "It should work.";
	char* p = MY_SUBSTR(p,x,3,6);
        ... do something with p ...
        delete[] p; //delete it again
	return 0;
}

Don't worry if you can't understand the last two.

What a cool code man, thank you very very much you saved me.
You'r rock. BTW, this was a stupid project to convert string function to array of char.


Wish you the Best,
Best Regards,

>>Or even better:

I beg to differ. Not only is that hard to read, but also harder to understand, plus
it leave the user to remember to delete the object returned.

@OP: I advise you to use string, but if this is for learning purposes, then you
can do something like this :

void substr(const char *src, char *dst, int start, int len){
	/* Do error check here */
	int i = 0;
	while( start != len){
		dst[i++] = src[start++];
	}	
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.