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

user438383
  • 5,716
  • 8
  • 28
  • 43
  • 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. – sweenish Nov 07 '21 at 14:48
  • Related/dupe: [Why do strings in C need to be null terminated?](https://stackoverflow.com/questions/2221304/why-do-strings-in-c-need-to-be-null-terminated) – Yksisarvinen Nov 07 '21 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. – sweenish Nov 07 '21 at 14:52
  • I'd suggest std::string – Kenny Ostrom Nov 07 '21 at 15:02
  • [`std::string`](https://en.cppreference.com/w/cpp/string/basic_string) and [`std::span`](https://en.cppreference.com/w/cpp/container/span) – bolov Nov 07 '21 at 15:03
  • 1
    Does this answer your question? [Why is a null terminator necessary?](https://stackoverflow.com/questions/17371157/why-is-a-null-terminator-necessary) – Karl Knechtel Nov 07 '21 at 15:09

1 Answers1

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);
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
mevets
  • 10,070
  • 1
  • 21
  • 33
  • A good answer will mention the memory leak. – Bathsheba Nov 07 '21 at 17:37
  • @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. – mevets Nov 07 '21 at 18:23
  • I am not sure of the original formatting was intentional. Many new users don't know how to properly format a code block. – drescherjm Nov 07 '21 at 22:29
  • My take is that the answer should incorporate best practices whenever possible. Given the question, I doubt a diff is happening. – sweenish Nov 08 '21 at 00:18
  • It isn't an answer to how a substring function should be written. It is an answer to `why is *this* program not working`. – mevets Nov 08 '21 at 00:41