2
\$\begingroup\$

I am a complete noob to C and I've shied away from using such a powerful language due to it's large learning curve and other complicated mechanisms. For my first minor project I plan to create a small HTTP client that requests for a webpage. Before I start, I need to create a function that can correctly parse a URL using the URI scheme. I devised this function to help me separate http from a host name like www.google.com by recognizing ://. Space and clock efficiency is are priorities of mine. I'm looking for any tips that would help someone who is new to C.

int delimiateString(const char *string, const char *delimiator) {
    const char *buffer;
    int x, len = strlen(delimiator);
    for (buffer = string; *buffer != '\0'; buffer++) {
        if (*delimiator == *buffer) {
            for (x = 1; x <= len; x++) {
                if (*(delimiator+x) == *(buffer+x)) {
                    if (x+1 == len) {
                        return (buffer - string);
                    }
                } else {
                    break;
                }
            }
        }
    }
    return strlen(string);
}
\$\endgroup\$
4
  • \$\begingroup\$ strchr will search through the entire delimiator. That's not what you want. You want to check only the first letter. So you should replace if(strchr(delimiator, *buffer) != NULL with if(*delimiator == *buffer). Or just just strcmp instead. But why do you use C for an HTTP client? That would be a lot easier in many other languages. \$\endgroup\$
    – SpiderPig
    Mar 8 '15 at 6:47
  • \$\begingroup\$ Okay, will do. I'm using C just to learn the language. \$\endgroup\$
    – dylanweber
    Mar 8 '15 at 6:53
  • \$\begingroup\$ Can you show an example usage of this function? Would :// be the delimiter or would /? What does this function return? The usage seems a bit awkward to me, and I'm just wondering if there's something I'm missing. \$\endgroup\$
    – Corbin
    Mar 8 '15 at 6:55
  • \$\begingroup\$ Since I'm working on the ground up, I really don't have any full code but I only have a rough plan. char *text = "http://mhjj:popi@192.168.0.2:8888/servlet/rece/"; int i = delimiateString(text, "://"); is the intended use currently and it would help me get the appropriate length (from the beginning of the string) to allocate the protocol string "http" and to help me extract the remaining portion of text that would need to be parsed. \$\endgroup\$
    – dylanweber
    Mar 8 '15 at 6:59
1
\$\begingroup\$

Your function seems to do this:

  • Find the first occurrence of "delimiator" (delimiter?) in some string. For example find the first occurrence of :// in http://example.com.
  • If found, return the position where it was found
  • If not found, return the length of the input string

To find if a string is part of another string, you can use the strstr function. This is equivalent to your code but simpler:

int delimiateString(const char *string, const char *delimiator) {
    char * found = strstr(string, delimiator);
    return found != NULL ? found - string : strlen(string);
}

For the record, your original code can be simplified, by eliminating an if around the inner for loop, and rearranging the nested ifs at the deepest level:

for (buffer = string; *buffer != '\0'; buffer++) {
    for (x = 0; x < len; x++) {
        if (*(delimiator+x) != *(buffer+x)) {
            break;
        }
        if (x+1 == len) {
            return buffer - string;
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Thank you... I'm foolish to have not found strstr before. At least the time spent making that function was not to waste for I need the practice. \$\endgroup\$
    – dylanweber
    Mar 8 '15 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.