function to return word in a string that starts with http

Discussion in 'C Programming' started by joe, Feb 1, 2012.

  1. joe

    joe Guest

    Hello I would like to find the words that start with http on a string.
    can anyone tell me how to do this. let say the string is "here is the
    link http://www.somewhere.org and a few more words"
    I would like to find the word that starts with http. In this case the
    full URL. How can I do this?

    I tried


    char type[32], *dot;


    while ((dot = strrchr (type, ' ')) != NULL) {

    but that will put in dot only the last occurance after the space.

    any suggestions? Thanks.
    joe, Feb 1, 2012
    #1
    1. Advertising

  2. joe

    John Gordon Guest

    In <> joe <> writes:

    > Hello I would like to find the words that start with http on a string.
    > can anyone tell me how to do this. let say the string is "here is the
    > link http://www.somewhere.org and a few more words"
    > I would like to find the word that starts with http. In this case the
    > full URL. How can I do this?


    > I tried


    > char type[32], *dot;


    > while ((dot = strrchr (type, ' ')) != NULL) {


    > but that will put in dot only the last occurance after the space.


    > any suggestions? Thanks.


    Use the strstr() function.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main(void)
    {
    char *sentence = "the link is http://www.somewhere.org and more words";
    char *http;
    char *space;
    char url[999];

    http = strstr(sentence, "http");

    if(http)
    {
    strcpy(url, http);
    space = strchr(url, ' ');
    if(space)
    *space = 0;
    printf("The url is: %s\n", url);
    }
    else
    {
    printf("http not found in '%s'.\n", sentence);
    }

    return EXIT_SUCCESS;
    }

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Feb 1, 2012
    #2
    1. Advertising

  3. On Wed, 1 Feb 2012 11:48:51 -0800 (PST), joe <>
    wrote:

    >Hello I would like to find the words that start with http on a string.
    >can anyone tell me how to do this. let say the string is "here is the
    >link http://www.somewhere.org and a few more words"
    >I would like to find the word that starts with http. In this case the
    >full URL. How can I do this?
    >
    >I tried
    >
    >
    > char type[32], *dot;
    >
    >
    >while ((dot = strrchr (type, ' ')) != NULL) {
    >
    >but that will put in dot only the last occurance after the space.


    Finding the occurrence of "http" is relatively easy (e.g., strstr).
    Confirming that it is at the beginning of a ?word? is also relatively
    easy (e.g., testing the previous character with isspace and ispunct).
    To avoid finding occurrences which are not part of a url, you might
    want to search for "http:" or "http://" instead.

    However, finding the end of the url could be a bit trickier. A blank
    seems straightforward, as does a comma. A period not followed by a
    space is obviously not the end of the url while one that is probably
    marks the end of the sentence and thus the url also. But what about
    slashes, underscores, dashes and other non-alphanumeric characters
    which can appear in a url? How do you distinguish their use in a url
    versus their normal grammatical use?

    --
    Remove del for email
    Barry Schwarz, Feb 1, 2012
    #3
  4. joe

    Jorgen Grahn Guest

    On Wed, 2012-02-01, Barry Schwarz wrote:
    ....
    > However, finding the end of the url could be a bit trickier. A blank
    > seems straightforward, as does a comma. A period not followed by a
    > space is obviously not the end of the url while one that is probably
    > marks the end of the sentence and thus the url also. But what about
    > slashes, underscores, dashes and other non-alphanumeric characters
    > which can appear in a url? How do you distinguish their use in a url
    > versus their normal grammatical use?


    There's an RFC -- I forget the number -- which recommends using
    <url:http://example.org/foo> to mark URLs in otherwise unstructured
    text, like news postings.

    Of course the OP may be unable to enforce that.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Feb 3, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    2,623
  2. Torsten Mohr
    Replies:
    3
    Views:
    706
    Richard Thomas
    Dec 8, 2009
  3. x1
    Replies:
    11
    Views:
    263
  4. Greg Ma

    Word starts by a vowel

    Greg Ma, Oct 28, 2010, in forum: Ruby
    Replies:
    1
    Views:
    99
    Ammar Ali
    Oct 28, 2010
  5. Replies:
    7
    Views:
    81
Loading...

Share This Page