get the full string when a word matches

R

Rudra Banerjee

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

James Kuyper

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

BruceS

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
 
E

Eric Sosman

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

Barry Schwarz

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:"
 
M

Malcolm McLean

בת×ריך ×™×•× ×©×œ×™×©×™, 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.
 
B

Ben Bacarisse

Malcolm McLean said:
בת×ריך ×™×•× ×©×œ×™×©×™, 18 בספטמבר 2012 23:59:57 UTC+1, מ×ת Rudra Banerjee:

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.

<snip>
 

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

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top