Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #16613

Re: function to return word in a string that starts with http

From John Gordon <gordon@panix.com>
Newsgroups comp.lang.c
Subject Re: function to return word in a string that starts with http
Date 2012-02-01 20:18 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <jgc6m4$lga$1@reader1.panix.com> (permalink)
References <f27103de-81ac-40f1-b685-06b76d70cf18@n6g2000vbz.googlegroups.com>

Show all headers | View raw


In <f27103de-81ac-40f1-b685-06b76d70cf18@n6g2000vbz.googlegroups.com> joe <jcharth@gmail.com> 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
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

function to return word in a string that starts with http joe <jcharth@gmail.com> - 2012-02-01 11:48 -0800
  Re: function to return word in a string that starts with http John Gordon <gordon@panix.com> - 2012-02-01 20:18 +0000
  Re: function to return word in a string that starts with http Devil with the China Blue Dress <chine.bleu@yahoo.com> - 2012-02-01 12:54 -0800
  Re: function to return word in a string that starts with http Barry Schwarz <schwarzb@dqel.com> - 2012-02-01 15:19 -0800
    Re: function to return word in a string that starts with http Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-03 21:00 +0000

csiph-web