Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
srivastavHimanshu4503 opened this issue Jul 15, 2024 · 1 comment

Comments

@srivastavHimanshu4503
Copy link

#include <stdio.h>

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
Copy link
Author

The following code is working completely right.
#include <stdio.h>

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.

@srivastavHimanshu4503 srivastavHimanshu4503 changed the title The following code does not work as intended. The following code does not work as intended. Chapter 8-Practice Set Question Number 4 Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant