CodeWithHarry / The-Ultimate-C-Programming-Course

This is the C language code and supplement material for the Ultimate C language Course on CodeWithHarry
220 stars 60 forks source link

The following code does not work as intended. Chapter 8-Practice Set Question Number 4 #5

Open srivastavHimanshu4503 opened 1 month ago

srivastavHimanshu4503 commented 1 month ago

include

char slice(char str[], int m, int n){ int i=0, count;
char
ptr1 = &str[m]; char *ptr2 = &str[n];

str = ptr1;
str[n] = '\0';
return str;

} int main(){ char str[] = "Himanshu Srivastav";

printf("%s", slice(str, 6, 11));
// Desired Output : hu Sri
// Actual output: hu Srivasta (Wrong)
return 0;

}

srivastavHimanshu4503 commented 1 month ago

The following code is working completely right.

include

char* slice(char str[], int m, int n){ str = &str[m]; str[n-m+1] = '\0';

return str;

} int main(){ char str[] = "Himanshu Srivastav"; int m = 6; int n = 11;

printf("%s\n", slice(str, 6, 11));
return 0;

} Hope this will implemented soon.