Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   get the full string when a word matches (http://www.velocityreviews.com/forums/t952389-get-the-full-string-when-a-word-matches.html)

Rudra Banerjee 09-18-2012 10:59 PM

get the full string when a word matches
 
Sorry for posting two problem back to back.
I have a file that contains line like:
(2010) 4287-4293. doi:10.1016/j.physb.2010.07.028
etc.
where the doi:* is of importance to me.
is there any way in C to get the string(even from the middle of line) begins with doi: and delimited only by space or newline?

James Kuyper 09-19-2012 12:20 AM

Re: get the full string when a word matches
 
On 09/18/2012 06:59 PM, Rudra Banerjee wrote:
> Sorry for posting two problem back to back.
> I have a file that contains line like:
> (2010) 4287-4293. doi:10.1016/j.physb.2010.07.028
> etc.
> where the doi:* is of importance to me.
> is there any way in C to get the string(even from the middle of line) begins with doi: and delimited only by space or newline?


Yes, the key tools you'll need are strchr() and strstr(). Precisely how
to use them depends upon precisely what the format is of the string. Do
spaces and newlines delimit the beginning of the doi:, as well as the
end of the string? Can "doi:" ever occur at the beginning of the string?
--
James Kuyper

BruceS 09-19-2012 12:24 AM

Re: get the full string when a word matches
 
On Tue, 18 Sep 2012 15:59:57 -0700, Rudra Banerjee wrote:

> Sorry for posting two problem back to back.
> I have a file that contains line like:
> (2010) 4287-4293. doi:10.1016/j.physb.2010.07.028 etc.
> where the doi:* is of importance to me.
> is there any way in C to get the string(even from the middle of line)
> begins with doi: and delimited only by space or newline?


I'm not entirely clear on what you're looking for, but I think strstr()
is probably a good place to start. It will return a pointer to the first
place in a string where another string is found. So, for instance, if
you give it the line above and "doi", you'll get
"doi:10.1016/j.physb.2010.07.028 etc.". If it doesn't find "doi" in the
input string, it will return null, so you'll know the line doesn't
contain "doi". If you want just the part from "doi" up to the first
space, you can then strchr() to find that space and set it to '\0'.
HTH

Eric Sosman 09-19-2012 12:27 AM

Re: get the full string when a word matches
 
On 9/18/2012 6:59 PM, Rudra Banerjee wrote:
> Sorry for posting two problem back to back.
> I have a file that contains line like:
> (2010) 4287-4293. doi:10.1016/j.physb.2010.07.028
> etc.
> where the doi:* is of importance to me.
> is there any way in C to get the string(even from the middle of line) begins with doi: and delimited only by space or newline?


Yes, but your lone example doesn't give enough information
about the pattern ("What's the next number in 9 ...?").

Possible building blocks: The strstr() function to locate
occurrences of "doi:" in the string, the strtok() function to
divide the string into space-separated fields, the sscanf()
function to, well, to scan. And others, of course.

--
Eric Sosman
esosman@ieee-dot-org.invalid
"The speed at which the system fails is usually not important."

Barry Schwarz 09-19-2012 12:32 AM

Re: get the full string when a word matches
 
On Tue, 18 Sep 2012 15:59:57 -0700 (PDT), Rudra Banerjee
<bnrj.rudra@gmail.com> wrote:

>Sorry for posting two problem back to back.
>I have a file that contains line like:
>(2010) 4287-4293. doi:10.1016/j.physb.2010.07.028
>etc.
>where the doi:* is of importance to me.
>is there any way in C to get the string(even from the middle of line) begins with doi: and delimited only by space or newline?


You can use strstr to find the location of the "doi:". If it is
present:

You could use strcspn to find the length of the substring at that
address that does not contain either blank or newline.

You could use strpbrk to find the location of the first blank or
newline after the "doi:".

You could use strtok to terminate the substring at the first
blank or newline after the "doi:"

--
Remove del for email

Malcolm McLean 09-19-2012 06:32 PM

Re: get the full string when a word matches
 
בתאריך יום שלישי, 18 בספטמבר 2012 23:59:57 UTC+1, מאת Rudra Banerjee:
> Sorry for posting two problem back to back.
>
> is there any way in C to get the string(even from the middle of line) begins with doi: and delimited only by space or newline?


this gives you the rough idea.

char *doi;
char *end;
size_t len;
char *answer;

doi = strstr(str, "doi:");
end = doi;
while(*end && !isspace( (unsigned) *end))
end++;
len = end - doi - 4;
answer = malloc(len + 1);
memcpy(answer, doi+4, len);
answer[len] = 0;


you need to decide what to do if the string doesn't contain doi, contains two
or if the date is clearly not a vaild doi.

Ben Bacarisse 09-19-2012 07:33 PM

Re: get the full string when a word matches
 
Malcolm McLean <malcolm.mclean5@btinternet.com> writes:

> בתאריך יום שלישי, 18 בספטמבר 2012 23:59:57 UTC+1, מאת Rudra Banerjee:
>> Sorry for posting two problem back to back.
>>
>> is there any way in C to get the string(even from the middle of line) begins with doi: and delimited only by space or newline?

>
> this gives you the rough idea.
>
> char *doi;
> char *end;
> size_t len;
> char *answer;
>
> doi = strstr(str, "doi:");
> end = doi;
> while(*end && !isspace( (unsigned) *end))


I think you meant (unsigned char) here.

> end++;


<snip>
--
Ben.


All times are GMT. The time now is 03:50 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57