Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > substring

Reply
Thread Tools

substring

 
 
Jupiter
Guest
Posts: n/a
 
      11-02-2003
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...
 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      11-02-2003

"Jupiter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Peter Pichler
Guest
Posts: n/a
 
      11-02-2003
"Jupiter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
Andreas Kahari
Guest
Posts: n/a
 
      11-02-2003
In article <(E-Mail Removed) >, 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
 
Reply With Quote
 
Peter Pichler
Guest
Posts: n/a
 
      11-02-2003

"Peter Pichler" <(E-Mail Removed)> wrote in message
news:UO4pb.422$(E-Mail Removed)...
> 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!


 
Reply With Quote
 
Ed Morton
Guest
Posts: n/a
 
      11-02-2003


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.

 
Reply With Quote
 
Tristan Miller
Guest
Posts: n/a
 
      11-02-2003
Greetings.

In article <3fa4d902$(E-Mail Removed)>, 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
 
Reply With Quote
 
Al Bowers
Guest
Posts: n/a
 
      11-02-2003


Tristan Miller wrote:
> Greetings.
>
> In article <3fa4d902$(E-Mail Removed)>, 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: http://www.velocityreviews.com/forums/(E-Mail Removed) (remove the x to send email)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      11-02-2003
Tristan Miller wrote:

> Greetings.
>
> In article <3fa4d902$(E-Mail Removed)>, 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 : (E-Mail Removed)
"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
 
Reply With Quote
 
Tristan Miller
Guest
Posts: n/a
 
      11-02-2003
Greetings.

In article <bo3mqn$6k7$(E-Mail Removed)>, 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding a SubString within a String Badass Scotsman ASP .Net 2 03-31-2006 04:00 PM
RegEx search for a substring within a substring colinhumber@gmail.com Perl Misc 3 08-03-2005 04:29 PM
find if there is a given substring inside a string juli ASP .Net 3 12-06-2004 11:52 AM
web sddress substring =?Utf-8?B?Sm9u?= ASP .Net 2 11-30-2004 01:31 PM
"Substring" for images? How do I determine if an image is contained within another? Sean Java 0 05-04-2004 05:50 PM



Advertisments