in Education by
In the code below, in "parse" function I am trying to get substring from the string "line". I am successfully printing the "method" variable, but "requesttarget" and "httpversion" variables are empty for some reason. (ps all these printf's are also inside my parse function) #include #include #include #include //prototypes bool parse(const char* line, char* abs_path, char* query); int strindex(char** pos, const char* str); void substr(int start, int end, char* holder, const char* line); int main(void) { const char* line = "GET /hello.php?name=Alice HTTP/1.1"; char* abs_path = NULL; char* query = NULL; if(parse(line, abs_path, query)) { printf("It works!\n"); } } bool parse(const char* line, char* abs_path, char* query) { char* space; int firstspace; int secondspace; char* method = malloc(50 * sizeof(char)); char* requesttarget = malloc(50 * sizeof(char)); char* httpversion = malloc(50 * sizeof(char)); space = strchr(line, ' '); printf("%p\n", space); //checks if strchr returns if(space == NULL) { return false; } //index in INT of the character firstspace = strindex(&space, line); printf("%i\n", firstspace); //stores the method substr(0, firstspace, method, line); space = strrchr(line, ' '); printf("%p\n", space); //index in INT of the character secondspace = strindex(&space, line); printf("%i\n", secondspace); //checks if strchr returns if(space == NULL) { return false; } //firstspace should come before secondspace if(firstspace > secondspace) { return false; } //stores request - target substr(firstspace + 1, secondspace, requesttarget, line); //stores http-version substr(secondspace + 1, strlen(line), httpversion, line); printf("method: %s\n", method); printf("requesttarget: %s\n", requesttarget); printf("httpversion: %s\n", httpversion); return true; } int strindex(char** pos, const char* str) { for(int i = 0, n = strlen(str); i < n; i++) { if((str + i) == *pos) { return i; } } return -1; } void substr(int start, int end, char* holder, const char* line) { //char* holder = malloc(50 * sizeof(char)); int i = start; for(; i < end; i++) { holder[i] = line[i]; } holder[i] = '\0'; //return holder; } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
void substr(int start, int end, char* holder, const char* line) { //char* holder = malloc(50 * sizeof(char)); int i = start, j=0; for(; i < end; i++) { holder[j++] = line[i]; } holder[j] = '\0'; //return holder; } you were not storing data in holder from 2nd iteration properly. from 2nd iteration start = 3 and end = 25. While storing in holder your index starts from 3, which is correct for line but not for holder. Add one more variable to start the index for holder from 0.

Related questions

0 votes
    In the code below, in "parse" function I am trying to get substring from the string "line". I am ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 5, 2022 in Education by JackTerrance
0 votes
    How do I write a function to split and return an array for a string with delimiters in the C ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    I having a trouble with an adwords API and wondering if you can help me. I called adwords api ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    I having a trouble with an adwords API and wondering if you can help me. I called adwords api ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I'm currently developing my first python program, a booking system using tkinter. I have a customer account ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    In my unit test I am calling the following: every { connectivityManager.activeNetworkInfo } returns null but ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    This is my code it's just starting the scan but it is not completing ,where is the error in it. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I am trying to make an app that can get the patient's symptoms as inputs and outputs the three most likely ... classes[sortedshit[2]]) Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    sumit is trying his hand at java programming but ,he does not know which type of language it is Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
0 votes
    I have a string like (&(objectclass=${abc})(uid=${xyz})) How can I search ${abc} and $ ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have a string like (&(objectclass=${abc})(uid=${xyz})) How can I search ${abc} and $ ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have a string like (&(objectclass=${abc})(uid=${xyz})) How can I search ${abc} and $ ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have a string like (&(objectclass=${abc})(uid=${xyz})) How can I search ${abc} and $ ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Can someone tell me any string.contains or string.indexof method in Python I want to do if not somestring.contains("boom"): continue Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    I have the below string "Halo Device ANumber : 6232123123 Customer ID : 16926" I want to search with the ... 22 substring : Device Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
...