Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > return the start of a substring in a string in c

Reply
Thread Tools

return the start of a substring in a string in c

 
 
yinglcs@gmail.com
Guest
Posts: n/a
 
      07-14-2007
Hi,
In java, there is an 'indexOf' function in String which does this:

indexOf(String str)
Returns the index within this string of the first occurrence
of the specified substring.

is there anything like that in c?

The closest thing I can find is strchr, but it check for a character,
not a substring?

Thank you.

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      07-14-2007
said:

> Hi,
> In java, there is an 'indexOf' function in String which does this:
>
> indexOf(String str)
> Returns the index within this string of the first occurrence
> of the specified substring.
>
> is there anything like that in c?
>
> The closest thing I can find is strchr, but it check for a character,
> not a substring?


strstr returns a pointer to the substring - if this is non-NULL,
subtract the address of the main string from it to get the index.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      07-14-2007

"Richard Heathfield" <> wrote in message
news:tZ-...
> said:
>
>> Hi,
>> In java, there is an 'indexOf' function in String which does this:
>>
>> indexOf(String str)
>> Returns the index within this string of the first occurrence
>> of the specified substring.
>>
>> is there anything like that in c?
>>
>> The closest thing I can find is strchr, but it check for a character,
>> not a substring?

>
> strstr returns a pointer to the substring - if this is non-NULL,
> subtract the address of the main string from it to get the index.
>

ie;

char *str = "astring";
char *substring = "in";
char *ptr;
ptrdiff_t index; /* should be an int. Horrid horrid horrid */

ptr = strstr(str, substring);
if(ptr)
{
index = ptr - str;
printf("your substring was found at postion %d (0-based)\n", (int)
index);
}

As an exercise to the regs, why do we need that horrid, horrid cast in the
call to printf?

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


 
Reply With Quote
 
Harald van =?UTF-8?B?RMSzaw==?=
Guest
Posts: n/a
 
      07-14-2007
Malcolm McLean wrote:
> char *str = "astring";
> char *substring = "in";
> char *ptr;
> ptrdiff_t index; /* should be an int. Horrid horrid horrid */
>
> ptr = strstr(str, substring);
> if(ptr)
> {
> index = ptr - str;
> printf("your substring was found at postion %d (0-based)\n", (int)
> index);
> }
>
> As an exercise to the regs, why do we need that horrid, horrid cast in the
> call to printf?


You don't. You're already assuming that index will fit in an int, so you can
just change the declaration of index to match.
 
Reply With Quote
 
Ralf Kunz
Guest
Posts: n/a
 
      07-14-2007
schrieb:
> Hi,
> In java, there is an 'indexOf' function in String which does this:
>
> indexOf(String str)
> Returns the index within this string of the first occurrence
> of the specified substring.
>
> is there anything like that in c?
>
> The closest thing I can find is strchr, but it check for a character,
> not a substring?
>
> Thank you.
>


hello,

you can use instead the function
char * strstr(const char *s1, const char *s2)

from string.h

Ralf
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      07-14-2007

"Harald van D?k" <> wrote in message
news:f79r9j$uaq$...
> Malcolm McLean wrote:
>> char *str = "astring";
>> char *substring = "in";
>> char *ptr;
>> ptrdiff_t index; /* should be an int. Horrid horrid horrid */
>>
>> ptr = strstr(str, substring);
>> if(ptr)
>> {
>> index = ptr - str;
>> printf("your substring was found at postion %d (0-based)\n", (int)
>> index);
>> }
>>
>> As an exercise to the regs, why do we need that horrid, horrid cast in
>> the
>> call to printf?

>
> You don't. You're already assuming that index will fit in an int, so you
> can
> just change the declaration of index to match.
>

My point exactly. We fill the language with gibberish types, which don't
actually gain anything because eventually some fucntion somewhere will
expect an int.
Join the campaign for 64 bit ints and this nonsense will fade away.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
Harald van =?UTF-8?B?RMSzaw==?=
Guest
Posts: n/a
 
      07-14-2007
Malcolm McLean wrote:
> "Harald van D?k" <> wrote in message
> news:f79r9j$uaq$...
>> Malcolm McLean wrote:
>>> char *str = "astring";
>>> char *substring = "in";
>>> char *ptr;
>>> ptrdiff_t index; /* should be an int. Horrid horrid horrid */
>>>
>>> ptr = strstr(str, substring);
>>> if(ptr)
>>> {
>>> index = ptr - str;
>>> printf("your substring was found at postion %d (0-based)\n", (int)
>>> index);
>>> }
>>>
>>> As an exercise to the regs, why do we need that horrid, horrid cast in
>>> the
>>> call to printf?

>>
>> You don't. You're already assuming that index will fit in an int, so you
>> can
>> just change the declaration of index to match.
>>

> My point exactly. We fill the language with gibberish types, which don't
> actually gain anything because eventually some fucntion somewhere will
> expect an int.


Which function is that, here? You don't need to assume index will fit in an
int, you just chose to for convenience.
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      07-14-2007
<> wrote in message
news: oups.com...
> Hi,
> In java, there is an 'indexOf' function in String which does this:
>
> indexOf(String str)
> Returns the index within this string of the first occurrence
> of the specified substring.
>
> is there anything like that in c?
>
> The closest thing I can find is strchr, but it check for a character,
> not a substring?


If you know strchr() searches a string for a char, did it not occur to you
to check if strstr() searches a string for a string?

However, both return a pointer to the first instance found (or NULL, if
none) instead of an index. If the returned value was non-NULL, subtract the
pointer to the original string from the returned value to get the index.

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      07-14-2007

"Harald van Dijk" <> wrote in message
news:f7a29h$bd$...
> Malcolm McLean wrote:
>> "Harald van D?k" <> wrote in message
>> news:f79r9j$uaq$...
>>> Malcolm McLean wrote:
>>>> char *str = "astring";
>>>> char *substring = "in";
>>>> char *ptr;
>>>> ptrdiff_t index; /* should be an int. Horrid horrid horrid */
>>>>
>>>> ptr = strstr(str, substring);
>>>> if(ptr)
>>>> {
>>>> index = ptr - str;
>>>> printf("your substring was found at postion %d (0-based)\n", (int)
>>>> index);
>>>> }
>>>>
>>>> As an exercise to the regs, why do we need that horrid, horrid cast in
>>>> the
>>>> call to printf?
>>>
>>> You don't. You're already assuming that index will fit in an int, so you
>>> can
>>> just change the declaration of index to match.
>>>

>> My point exactly. We fill the language with gibberish types, which don't
>> actually gain anything because eventually some fucntion somewhere will
>> expect an int.

>
> Which function is that, here? You don't need to assume index will fit in
> an
> int, you just chose to for convenience.
>

printf(). There probably is a specifier for ptrdiff_t in C99, but I doubt
you can guarantee its presence.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      07-14-2007
On Sat, 14 Jul 2007 07:29:35 +0100, Malcolm McLean wrote:

>
> "Richard Heathfield" <> wrote in message
> news:tZ-...
>> said:
>>
>>> Hi,
>>> In java, there is an 'indexOf' function in String which does this:
>>>
>>> indexOf(String str)
>>> Returns the index within this string of the first occurrence
>>> of the specified substring.
>>>
>>> is there anything like that in c?
>>>
>>> The closest thing I can find is strchr, but it check for a character,
>>> not a substring?

>>
>> strstr returns a pointer to the substring - if this is non-NULL,
>> subtract the address of the main string from it to get the index.
>>

> ie;
>
> char *str = "astring";
> char *substring = "in";
> char *ptr;
> ptrdiff_t index; /* should be an int. Horrid horrid horrid */
>
> ptr = strstr(str, substring);
> if(ptr)
> {
> index = ptr - str;
> printf("your substring was found at postion %d (0-based)\n", (int)
> index);
> }
>
> As an exercise to the regs, why do we need that horrid, horrid cast in the
> call to printf?

Because you feared enough that the magnitude of (ptr - str) could
ever be > 32767, that you didn't declare index as an int.
Otherwise you could declare it as a long and use "%ld" in printf.
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

 
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
A simple 'Replace' and 'Substring' question: How to add a string to another string SM Javascript 4 04-27-2007 07:22 AM
RegEx search for a substring within a substring colinhumber@gmail.com Perl Misc 3 08-03-2005 04:29 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
RE: Isn't there a substring(start, end)-function???? Harvey Thomas Python 0 08-06-2003 01:37 PM
Isn't there a substring(start, end)-function???? Dave Python 3 08-06-2003 01:26 PM



Advertisments
 



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