Question
1.please write your code in c 2. your code will be judged by loader code 3. make sure to pass the test case As mentioned
1.please write your code in c
2. your code will be judged by loader code
3. make sure to pass the test case
As mentioned in windows.location, there are different information stored in different position of url. Now we are going to parse the content of search section, which is also known as query string.
A search part is composed of key/value pairs. The format of each pair is [key]=[value] and separated by &.
Following is an example url that contains multiple key/value pairs.
https://www.abc.com/photo?from=20180101&to=20201231#favorite
The search part of the url is from=20180101&to=20201231 that includes two pairs of key/value:
from=20180101
to=20201231
If there is duplicated key, the first key/value pair of the key should be kept. For instance, if we search foo with in foo=bar&foo=baz, the result will be bar.
It is possible that the value field is empty. Take foo=&bar=123 as example, the value of foo will be (empty string).
A common application of the search part of a url is for querying specific data. For example, with the following url:
http://www.abc.com/profile?userid=10
We may assume the profile of the user with userid is 10 will be returned from the host www.abc.com.
Please print the corresponding key/value pair of the given key from the provided url. If the required key does not exist, 404 Not Found should be printed.
The format of the url is: portocol://host[:port][/pathname][?search][#hash], where the components enclosed by [] are optional, that is, may not be provided in the url.
Input
The first string indicates url, which is composed of 10-2048 characters.
The second string indicates key, which is composed of 1-100 characters.
Output
If the key can be found in url, print key/value with following format:
key:[key], value[value]
Otherwise, print 404 Not Found.
Loader Code
Your code will be judge using this program:
#include
#include
#include
#define URL_MAX 2048
#define KEY_MAX 100
typedef struct search_s {
char *key;
char *value;
} search_t;
search_t *get_param_by_key(char *url, char *key);
int main ()
{
char url[URL_MAX + 1], key[KEY_MAX + 1];
fgets(url, URL_MAX, stdin);
scanf("%s", key );
search_t *param = get_param_by_key (url, key);
if (param)
printf("key: %s, value: %s", param->key, param->value);
else
printf("404 Not Found");
}
Example 1
Input
https://web.ncku.edu.tw/index.php?Lang=zh-tw
language
Output
404 Not Found
Example 2
Input
http://test.com/?pet=cat&pet=dog&userid=1
pet
Output
key: pet, value: cat
Example 3
Input
https://test/haha/
key
Output
404 Not Found
Example 4
Input
http://127.0.0.1/?meow=zoo&bar=123&zoo=456#shop
zoo
Output
key: zoo, value: 456
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started