function to return word in a string that starts with http

J

joe

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.
 
J

John Gordon

In said:
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?
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;
}
 
B

Barry Schwarz

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?
 
J

Jorgen Grahn

.
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top