-
URL splitter
So I have this assignment for C course. I am quite lost about where to even start. Any help would be appreciated.
Read a URL (assume http protocol) as a string from stdin. Extract from the URL, the domain name, relative path name, name-value pairs in the query string. Print the following with appropriate ids.
URL
domain name
path name
the name-value pairs in the form "(name, value)"
You need to organize the program so that each task is accomplished by a function with minimal code clustering in the main program. For this assignment assume that URL has the format "http://...[?...=...&...&...=...]". The string that follows the ? is called query-string. It consists of name-value pairs separated by '&'. The name and the corresponding value are separated b '='. You may assume that the URLS are similar to the ones shown in the examples below. Assume that the valid URLS will have "https://" as a prefix. If the prefix is missing, treat it as an incorrect URL, print that with an error message, and continue to process the next line of input. You must implement and use the folliwng functions.
split(s,c,sl,sr) - splits a string s into two substring sl and sr. sl is the portion of the string to the left of c and sr is the portion following c. if the string does not contain c, sl is a copy of s and sr is empty string. This is a utility function called by other ........ions. It returns true if c is found in s, otherwise it returns false. s and c are input paramenters, and sl and sr are output parameters. Assume that all parameters are strings.
get_name_value(inp,name,value) - inp is input parameter (in general) of the form "name=value". name and value are output parameters. this function returns true if the string inp is of the correct form, otherwise retruns false. All parameters must be strings.
get_name_value_pair(s,nvp,tail) - splits a string s into two substrings nvp and tail. nvp is the portion of s preceding the first '&' in s, and tail is the substring following '&'. It returns true if s contains an '&', otherwise false. All must be strings.
get_domain(s,d) - extracts the domain d, from s
get_path(s,p) - extracts the path p from s
valid(s) - checks if the prefix of s is "http://". if the prefix is "http://" return 1, else return 0.
the functions get_name_value, get_name_value_pair, get_domain, and get_path must use the function split to extract the component strings.
You must use only the parameters specified.
assume one url at a time.
example
input - http://www.google.com/search?hl=en&q=ibm
output -
URL: http://www.google.com/search?hl=en&q=ibm
Domain name: http://www.google.com
relative path name: search
name-value pairs are as follows:
(hl,en)
(q,ibm)
-
You have the same assignment as I do.......are you in OSU c/c++ programming class?
-
What have you done so far?
-
I have the same assignment as this user and I started out with
Code:
split(char s; char c; char sl; char sr)
int main(void)
{
int i = 0;
char sl, sr, s, c;
char *chptr, name, value;
char sentence[15][80], a[10], b[100];
printf("Please enter a URL.");
fgets(sentence, 80, stdin);
for(chptr = sentence; *chptr != '\n'; chptr++);
*chptr = '\0';
while(i>7)
{
sentence[i] = a[i];
i++;
}
if(strcmp(a[i], "http://"))
{
exit(0);
}
while(sentence[i] != b[i])
{
sentence[i] = b[i];
i++;
}
printf("The domain name:", b[i]);
I'm not sure if this is correct or not
-
yeah same class, whats your email crazyazn? or shoot me a pm.
-
If you guys are in the same class I want to warn you that while working together is okay (in my opinion, I don't know about your instructor), sharing your entire code like CrazyAzn might get you into trouble.
-
I'm not sharing......plus the code that I typed up there is possibly wrong. I'm confused on the program assignment. I have no idea what to do....I want to know if my code up there is write or not.
-
-
Quote:
Originally Posted by
rags_to_riches
It's not right at all.
ha-ha.....*sigh* I'm tired.....made a typo :p
-
I changed the program and I want to know if I'm going in the right track. If so how do I complete the assignment?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 100
int split(char s[],char c[],char sl[],char sr[])
{
}
int get_name_value(char inp,char name,char value)
{
printf("Enter URL:");
fgets(userInput, sizeof(userInput), stdin);
}
int get_name_value_pair(char s,char nvp,char tail)
{
}
int get_domains(char s,char d)
{
}
int get_paths(char s,char p)
{
}
int valid(char s)
{
s = "http://";
char sCheck[7];
char validUrl[] = "http://";
int i = 0;
for (i=0;i<7;i++){
sCheck[i]=s;}
if (strcmp(sCheck,validUrl))
printf("valid URL");
else
printf("Invalid URL");
}
int main(void)
{
char stringName[stringSize];
int intArrayName[intArraySize];
int methodName(char stringName[], int intArrayName[]);
int i = 0, a[7], b[100];
char userInput[80];
printf("URL: ");
printf("\nDomain name: ");
printf("\nRelative path name: ");
printf("\nName-Value pairs are as follows: \n");
system("pause");
}
-
You need to take this one step at a time, or else you'll never make any progress. You need to compile AND TEST your code at each step. Right now you have a bunch of code with errors all over the place that won't even compile.
First, see if you can read in the user input and print it back out.