0

I have written a substring function that takes a value char array, initial value, and length and then return a substring

char* substring(char t[],int i,int l){ int k=0; char* subs=new char[l]; while(t[k]!=0){ if(k==i){ int a=i; int j=0; // for (int j=0;j<l;j++) while(j<l) { subs[j]=t[a]; a++; j++; } if(subs!=0){ break; } } k++; } return subs; } // CHECKING int main(){ char t[20]="this is a string"; cout<<substring(t,0,4); } //OUTPUT this└ 

everything is working properly getting exact output which I want but at the end of the output value it also return a unexpected value such as symbols and random alphabets don't know how to get rid of it **NOTE I don't want to use strings or anything else just want to clear the problem which in this programme

6
  • 6
    C-strings are null terminated. The fact that you ignored this is why it's advised to not use C-strings. You're also trying to return the array with just the pointer. This is just not going to end well, generally. Commented Nov 7, 2021 at 14:48
  • Related/dupe: Why do strings in C need to be null terminated? Commented Nov 7, 2021 at 14:49
  • Your while loops also look bad. Why check if you're starting at the right place? Why not just start at the right place? Maybe do some basic bounds checking before going into a loop and potentially accessing memory you have no business with. Use meaningful variable names so the code is more human-readable. And proper formatting as the cherry on top. Commented Nov 7, 2021 at 14:52
  • I'd suggest std::string Commented Nov 7, 2021 at 15:02
  • 1
    Does this answer your question? Why is a null terminator necessary? Commented Nov 7, 2021 at 15:09

1 Answer 1

0

This is about the smallest change to fix your function -- changing the allocation size, and terminating the substring. You might want to consider whether the outer loop is worthwhile -- shouldn't you return an empty string rather than an uninitialized one if the substring starts after the end of the string? Or should you return a null to signify the error? It is a matter of style, but the inner loop might be easier to read as: for (j = 0; j < l; j++) { subs[j] = t[i+j]; }.

char* substring(char t[], int i, int l) { int k = 0; char* subs = new char[l + 1]; while (t[k] != 0) { if (k == i) { int a = i; int j = 0; // for (int j=0;j<l;j++) while (j < l) { subs[j] = t[a]; a++; j++; } subs[j] = '\0'; break; } k++; } return subs; } // CHECKING int main() { char t[20] = "this is a string"; cout << substring(t, 0, 4); } 
Sign up to request clarification or add additional context in comments.

5 Comments

A good answer will mention the memory leak.
@drescherjm Although it is prettier to most, reformatting code hobbles diff; the way it was before, the OP can diff the source to the original to highlight the changes.
I am not sure of the original formatting was intentional. Many new users don't know how to properly format a code block.
My take is that the answer should incorporate best practices whenever possible. Given the question, I doubt a diff is happening.
It isn't an answer to how a substring function should be written. It is an answer to why is *this* program not working.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.