Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   substring (http://www.velocityreviews.com/forums/t316034-substring.html)

Jupiter 11-02-2003 09:57 AM

substring
 
Hey,
I would like to know 2 things.
1)Is there any function (in C standard library) that extracts a
substring from a string?
2)Is there any function (in C standard library) that returns the
position of a substring in a string?



Thx a lot...

Ivan Vecerina 11-02-2003 10:15 AM

Re: substring
 

"Jupiter" <zervos01@yahoo.com> wrote in message
news:27e90ba7.0311020157.405fdc48@posting.google.c om...
> Hey,
> I would like to know 2 things.
> 1)Is there any function (in C standard library) that extracts a
> substring from a string?

Not directly -- this has to be emulated with a more general
function such as memcpy (how exactly depends on the
destination storage for the substring).

> 2)Is there any function (in C standard library) that returns the
> position of a substring in a string?

Yes:
char* pos = strstr(str_to_search_through,str_to_find);
(null if not found).

hth, Ivan
--
http://ivan.vecerina.com



Peter Pichler 11-02-2003 10:16 AM

Re: substring
 
"Jupiter" <zervos01@yahoo.com> wrote in message
news:27e90ba7.0311020157.405fdc48@posting.google.c om...
> Hey,
> I would like to know 2 things.
> 1)Is there any function (in C standard library) that extracts a
> substring from a string?


Yes.

> 2)Is there any function (in C standard library) that returns the
> position of a substring in a string?


Yes.

> Thx a lot...


The questions you presumably *wanted* to ask were "what is the function
to..." :-)

To extract a substring on positions n to m from a string s you can use

strncpy(dest, s + n, m - n + 1);
dest[m - n] = 0;

(or in short strncpy(dest, s + n, m - n + 1)[m - n] = 0;)

Look up strncpy in your manual to see how it works and why adding the
zero is necessary. You must also make sure beforehand that:
a) s is at leat n + 1 characters long and
b) you have enough space in dest for m - n + 1 characters.

To find a substring in a string you can use

char * position = strstr(substring, string);

If you want an offset rather than a pointer to the substring, use

size_t offset = position - string;

Again, look up strstr in your manual for reference. Please note that
strstr returns NULL if the substring is not foud in string, you must
check for that.



Andreas Kahari 11-02-2003 10:19 AM

Re: substring
 
In article <27e90ba7.0311020157.405fdc48@posting.google.com >, Jupiter wrote:
> Hey,
> I would like to know 2 things.
> 1)Is there any function (in C standard library) that extracts a
> substring from a string?


Yes, strncpy() can be made to copy a substring of one string
into another:

#include <string.h> /* for strncpy() */
#include <stdio.h> /* for printf() */

int
main(void)
{
char msg[] = "Hello World!";
char submsg[10]; /* Must be long enough */

/* Copy the substring "o W" from msg to submsg */
strncpy(submsg, &msg[4], 3);

/* Terminate the resulting string since strncpy() doesn't */
submsg[3] = '\0';

printf("msg[] = '%s'\nsubmsg[] = '%s'\n", msg, submsg);

return 0;
}


> 2)Is there any function (in C standard library) that returns the
> position of a substring in a string?


No, but you may use strstr() like this:

#include <string.h> /* for strstr() */
#include <stdio.h> /* for printf(), fprintf() */
#include <stddef.h> /* for ptrdiff_t, NULL */

int
main(void)
{
char msg[] = "Hello World!";
char *ptr;

ptrdiff_t ptrpos;

/* Locate the substring "o W" in msg */
ptr = strstr(msg, "o W");

if (ptr == NULL) {
fprintf(stderr, "Substring not found\n");
} else {
/* Calculate the position of ptr in msg */
ptrpos = ptr - &msg[0];

printf("Position of 'o W' in '%s' is %d\n", msg, ptrpos);
}

return 0;
}

>
>
>
> Thx a lot...


Wlcm a lot...


--
Andreas Kähäri

Peter Pichler 11-02-2003 10:20 AM

Re: substring
 

"Peter Pichler" <pichlo6@pobox.sk> wrote in message
news:UO4pb.422$wY2.323@newsfep1-gui.server.ntli.net...
> To find a substring in a string you can use
>
> char * position = strstr(substring, string);

....
> Again, look up strstr in your manual for reference.


Err, *I* should have looked up strstr in the manual. Swap the two
parameters around. Doh!



Ed Morton 11-02-2003 02:55 PM

Re: substring
 


Jupiter wrote:
> Hey,
> I would like to know 2 things.
> 1)Is there any function (in C standard library) that extracts a
> substring from a string?
> 2)Is there any function (in C standard library) that returns the
> position of a substring in a string?


Yes. Take a look at http://www-ccs.ucsd.edu/c/string.html for a list of
the string functions in string.h. Buying a copy of K&R 2 wouldn't be a
bad idea either...

Ed.


Tristan Miller 11-02-2003 06:06 PM

Re: substring
 
Greetings.

In article <3fa4d902$1@news.swissonline.ch>, Ivan Vecerina wrote:
>> 1)Is there any function (in C standard library) that extracts a
>> substring from a string?

>
> Not directly -- this has to be emulated with a more general
> function such as memcpy (how exactly depends on the
> destination storage for the substring).


Eh? You got something against strncpy() or something?

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you

Al Bowers 11-02-2003 07:31 PM

Re: substring
 


Tristan Miller wrote:
> Greetings.
>
> In article <3fa4d902$1@news.swissonline.ch>, Ivan Vecerina wrote:
>
>>>1)Is there any function (in C standard library) that extracts a
>>>substring from a string?

>>
>>Not directly -- this has to be emulated with a more general
>>function such as memcpy (how exactly depends on the
>>destination storage for the substring).

>
>
> Eh? You got something against strncpy() or something?
>


'Something' might be more appropriate than strncpy. It depends on
the definition of 'extract'. I interpret this to mean to remove a
substring from a string.

Take string:
char s[] = "Have a very good day"
and extract the substring "very " to make s the
string "Have a good day".

If this is the intent of the OP, then a solution would not involve
strncpy or memcpy. A function duo of strstr and memmove would
do the job.

--
Al Bowers
Tampa, Fl USA
mailto: xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


Richard Heathfield 11-02-2003 07:44 PM

Re: substring
 
Tristan Miller wrote:

> Greetings.
>
> In article <3fa4d902$1@news.swissonline.ch>, Ivan Vecerina wrote:
>>> 1)Is there any function (in C standard library) that extracts a
>>> substring from a string?

>>
>> Not directly -- this has to be emulated with a more general
>> function such as memcpy (how exactly depends on the
>> destination storage for the substring).

>
> Eh? You got something against strncpy() or something?


Well, as a substring extractor, it's suboptimal.

Consider, for example:

char foo[16];
char bar[16] = "abcdefghijklmno";

strncpy(foo, bar, 3);

At the end of this operation, foo does not contain a string. Oops.

--
Richard Heathfield : binary@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Tristan Miller 11-02-2003 08:51 PM

Re: substring
 
Greetings.

In article <bo3mqn$6k7$1@hercules.btinternet.com>, Richard Heathfield wrote:
> Consider, for example:
>
> char foo[16];
> char bar[16] = "abcdefghijklmno";
>
> strncpy(foo, bar, 3);
>
> At the end of this operation, foo does not contain a string. Oops.


Well, it doesn't contain a nul-terminated string, but it does contain the
first three characters of bar, which may be all that is needed in some
cases. Does the C standard use the term "string" to refer to both nul- and
non-nul-terminated strings, or does it make a nomenclatural distinction
between "string" (which always includes the sentinel) and the more general
"array-of-char"?

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you


All times are GMT. The time now is 07:33 PM.

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