Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

opposite of strchr()

552 views
Skip to first unread message

sinbad

unread,
Oct 30, 2012, 12:23:20 AM10/30/12
to
is there an equivalent lib api for opposite of strchr().
i referred the manual, i couldn't find one.

Barry Margolin

unread,
Oct 30, 2012, 1:26:10 AM10/30/12
to
In article <94e58990-7dba-406b...@googlegroups.com>,
sinbad <sinbad...@gmail.com> wrote:

> is there an equivalent lib api for opposite of strchr().
> i referred the manual, i couldn't find one.

What's the opposite of strchr()? I assume you don't mean strrchr(). Do
you mean you want to find the first character that ISN'T a specific
character? Maybe it's overkill, but a regexp library can do this easily
with the RE [^c], where c is the character.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Jérémie Courrèges-Anglas

unread,
Oct 30, 2012, 2:54:15 AM10/30/12
to
Barry Margolin <bar...@alum.mit.edu> writes:

> In article <94e58990-7dba-406b...@googlegroups.com>,
> sinbad <sinbad...@gmail.com> wrote:
>
>> is there an equivalent lib api for opposite of strchr().
>> i referred the manual, i couldn't find one.
>
> What's the opposite of strchr()? I assume you don't mean strrchr().

Indeed, too briefly specified.

> Do you mean you want to find the first character that ISN'T a specific
> character? Maybe it's overkill, but a regexp library can do this easily
> with the RE [^c], where c is the character.

In that case, take a look at strcspn().

--
Jérémie Courrèges-Anglas
GPG fingerprint : 61DB D9A0 00A4 67CF 2A90 8961 6191 8FBF 06A1 1494

Keith Thompson

unread,
Oct 31, 2012, 6:51:40 PM10/31/12
to
sinbad <sinbad...@gmail.com> writes:
> is there an equivalent lib api for opposite of strchr().
> i referred the manual, i couldn't find one.

strchr() finds the first occurrence of a character in a string.

I suppose the "opposite() of strchr()" would be a function that
*doesn't* find the first occurrence of a character in a string.

sqrt() is a good example of that. If sqrt() doesn't meet your
requirements, please state them more clearly.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Scott Lurndal

unread,
Nov 1, 2012, 10:09:28 AM11/1/12
to
Keith Thompson <ks...@mib.org> writes:
>sinbad <sinbad...@gmail.com> writes:
>> is there an equivalent lib api for opposite of strchr().
>> i referred the manual, i couldn't find one.
>
>strchr() finds the first occurrence of a character in a string.
>
>I suppose the "opposite() of strchr()" would be a function that
>*doesn't* find the first occurrence of a character in a string.

Or it could be a function that finds the _last_ occurance of a character
in a string, e.g. strrchr(). However, since that is described on the
same man page in most systems, it's probably not what the OP intended.

scott

sinbad

unread,
Nov 1, 2012, 11:09:00 AM11/1/12
to sl...@pacbell.net
strchr() finds the first occurrence of a certain character 'c'.
By opposite of strchr(), i mean, find the first character that
isn't 'c'. I apologize for the lack of clarity. strcspn() seems
to work for the need.

-sinbad

Bjarni Juliusson

unread,
Nov 1, 2012, 11:31:01 AM11/1/12
to
On 11/01/2012 03:09 PM, Scott Lurndal wrote:
> Keith Thompson<ks...@mib.org> writes:
>>
>>strchr() finds the first occurrence of a character in a string.
>>
>>I suppose the "opposite() of strchr()" would be a function that
>>*doesn't* find the first occurrence of a character in a string.
>
> Or it could be a function that finds the _last_ occurance of a character
> in a string, e.g. strrchr(). However, since that is described on the
> same man page in most systems, it's probably not what the OP intended.

Could be a function that takes a character and a string and loses the
character somewhere in the string.


Bjarni
--

INFORMATION WANTS TO BE FREE

Nathan Wagner

unread,
Oct 31, 2012, 9:14:15 PM10/31/12
to
On 2012-10-31, Keith Thompson <ks...@mib.org> wrote:
> sinbad <sinbad...@gmail.com> writes:
>> is there an equivalent lib api for opposite of strchr().
>> i referred the manual, i couldn't find one.
>
> strchr() finds the first occurrence of a character in a string.
>
> I suppose the "opposite() of strchr()" would be a function that
> *doesn't* find the first occurrence of a character in a string.

Not necessarily. "Opposite" here doesn't have to refer to
the finding. It could be the opposite of first, occurance, character,
or string.

So, it could find the last occurance, the first non-occurance,
the first non-character in a string (impossible I think, due
to the definition of a string), or the first character in a non-string
(also impossible I think, strings are the only objects that have
multiple characters, unless you want to somehow count byte
arrays).

IIRC, C has functions for the first two "opposites".

> sqrt() is a good example of that.

While I suppose that sqrt() doesn't do that, it isn't really
in the category of things that might. Which I imagine was
your point.

> If sqrt() doesn't meet your requirements, please state them more
> clearly.

Indeed. Though accurately stating requirements is sadly a rare
skill.

--
nw

Barry Schwarz

unread,
Nov 1, 2012, 3:13:28 PM11/1/12
to
Then you still haven't described what you really want.

strcspn("abcd", "a");
and
strcspn("aacd", "a");
both return 0. How does that help you find the 'b' in the first case
and the 'c' in the second?

--
Remove del for email

Keith Thompson

unread,
Nov 1, 2012, 6:56:35 PM11/1/12
to
Nathan Wagner <n...@hydaspes.if.org> writes:
> On 2012-10-31, Keith Thompson <ks...@mib.org> wrote:
>> sinbad <sinbad...@gmail.com> writes:
>>> is there an equivalent lib api for opposite of strchr().
>>> i referred the manual, i couldn't find one.
>>
>> strchr() finds the first occurrence of a character in a string.
>>
>> I suppose the "opposite() of strchr()" would be a function that
>> *doesn't* find the first occurrence of a character in a string.
>
> Not necessarily. "Opposite" here doesn't have to refer to
> the finding. It could be the opposite of first, occurance, character,
> or string.

Of course not *necessarily*.

> So, it could find the last occurance, the first non-occurance,
> the first non-character in a string (impossible I think, due
> to the definition of a string), or the first character in a non-string
> (also impossible I think, strings are the only objects that have
> multiple characters, unless you want to somehow count byte
> arrays).

And it's pointless to speculate until the OP clarifies his
requirements -- which he has (possibly after you posted this;
I didn't bother checking timestamps).

>> sqrt() is a good example of that.
>
> While I suppose that sqrt() doesn't do that, it isn't really
> in the category of things that might. Which I imagine was
> your point.

That was exactly my point.

[...]

Keith Thompson

unread,
Nov 1, 2012, 6:59:21 PM11/1/12
to
Barry Schwarz <schw...@dqel.com> writes:
> On Thu, 1 Nov 2012 08:09:00 -0700 (PDT), sinbad
> <sinbad...@gmail.com> wrote:
>>On Thursday, November 1, 2012 10:09:29 AM UTC-4, Scott Lurndal wrote:
>>> Keith Thompson <ks...@mib.org> writes:
>>> >sinbad <sinbad...@gmail.com> writes:
>>> >> is there an equivalent lib api for opposite of strchr().
>>> >> i referred the manual, i couldn't find one.
>>> >
>>> >strchr() finds the first occurrence of a character in a string.
>>> >
>>> >I suppose the "opposite() of strchr()" would be a function that
>>> >*doesn't* find the first occurrence of a character in a string.
>>>
>>>
>>> Or it could be a function that finds the _last_ occurance of a character
>>> in a string, e.g. strrchr(). However, since that is described on the
>>> same man page in most systems, it's probably not what the OP intended.
>>
>>strchr() finds the first occurrence of a certain character 'c'.
>>By opposite of strchr(), i mean, find the first character that
>>isn't 'c'. I apologize for the lack of clarity. strcspn() seems
>>to work for the need.
>
> Then you still haven't described what you really want.
>
> strcspn("abcd", "a");
> and
> strcspn("aacd", "a");
> both return 0. How does that help you find the 'b' in the first case
> and the 'c' in the second?

There likely isn't a predefined function that does what the OP wants,
but it would be nearly trivial to write one.

Barry Schwarz

unread,
Nov 1, 2012, 7:20:37 PM11/1/12
to
On Thu, 01 Nov 2012 15:59:21 -0700, Keith Thompson <ks...@mib.org>
wrote:

>Barry Schwarz <schw...@dqel.com> writes:
>> On Thu, 1 Nov 2012 08:09:00 -0700 (PDT), sinbad
>> <sinbad...@gmail.com> wrote:

snip

>>>strchr() finds the first occurrence of a certain character 'c'.
>>>By opposite of strchr(), i mean, find the first character that
>>>isn't 'c'. I apologize for the lack of clarity. strcspn() seems
>>>to work for the need.
>>
>> Then you still haven't described what you really want.
>>
>> strcspn("abcd", "a");
>> and
>> strcspn("aacd", "a");
>> both return 0. How does that help you find the 'b' in the first case
>> and the 'c' in the second?
>
>There likely isn't a predefined function that does what the OP wants,
>but it would be nearly trivial to write one.

Yes, but we are still left to wonder how the OP can claim that strcspn
satisfies his stated requirement. Either he has not stated the
requirement correctly or has not performed even the most rudimentary
testing.

sinbad

unread,
Nov 2, 2012, 1:35:18 AM11/2/12
to
On Friday, November 2, 2012 4:50:52 AM UTC+5:30, Barry Schwarz wrote:
> On Thu, 01 Nov 2012 15:59:21 -0700, Keith Thompson <ks...@mib.org>
>
> Yes, but we are still left to wonder how the OP can claim that strcspn
>
> satisfies his stated requirement. Either he has not stated the
>
> requirement correctly or has not performed even the most rudimentary
>
> testing.
>

i needed the opposite of strchr() in the following situation.
i need to process a set of commands entered by the user,
the commands might look like like "add node list", "add node queue"
"delete node list". I need to fetch each entry and take
appropriate actions, for example if the cmd is "add node list"
after fetchnig the first entry "add" i know tha t it's add function.
now i need to fetch the second entry which would be "node" but my
cmd pointer is pointing past the "d" in "add". I need to move beyond
all the spaces between "add" and "node" to reach the entry "node",
since the cmd is entered by the user there can be any number of
spaces between "add" and "node". i needed an api which will skip
all the spaces and get me to "node" if i pass the string " node".
strcspn() seems to work for me, if used like this.

strcspn(cmd, "abcdefghijklmnopqrstuvwxyz"));

where cmd is " node list"

i wrote my own function before i learnt about strcspn(),
but i prefer to use library routines if available.

-sinbad



Barry Schwarz

unread,
Nov 2, 2012, 2:32:37 AM11/2/12
to
It is good you were able to solve your problem.

The fact that it is a significantly different problem than the one in
your original post might explain why none of us thought to recommend
strcspn to you in the first place.

You might want to consider using strpbrk instead of strcspn to get
directly to the text you want. And strtok would do even more of the
work for you but it is not thread safe.

Mark

unread,
Nov 2, 2012, 5:09:27 AM11/2/12
to
!strchr()
--
(\__/) M.
(='.'=) If a man stands in a forest and no woman is around
(")_(") is he still wrong?

Keith Thompson

unread,
Nov 5, 2012, 2:22:46 PM11/5/12
to
sinbad <sinbad...@gmail.com> writes:
[...]
> i needed the opposite of strchr() in the following situation.
[snip]

A suggestion: Stop using the phrase "opposite of strchr()", and
instead just state what you need the function to do. You'll avoid
a lot of confusion.

Gordon Burditt

unread,
Nov 8, 2012, 7:27:19 PM11/8/12
to
> A suggestion: Stop using the phrase "opposite of strchr()", and
> instead just state what you need the function to do. You'll avoid
> a lot of confusion.

The opposite of strchr() clearly means a function that takes a character
as an argument and returns *all* strings containing that character (with
the minimum search space being the local filesystem and anything called
a "cloud", at least including any storage owned by Google or Amazon).

Keith Thompson

unread,
Nov 9, 2012, 3:42:58 PM11/9/12
to
gordon...@burditt.org (Gordon Burditt) writes:
> Keith Thompson <ks...@mib.org> writes: [attribution line added]
Gordon, I've told you before that you do not have my permission
to quote anything I write to Usenet without attribution. This has
not changed, and it applies to all newsgroups.

I have no idea why you don't just leave the attribution line
in place. (I know your stated rationale, but it makes no sense).
If you insist on inconveniencing yourself, go ahead. But if you
refuse to give credit where it's due, don't quote me.

Feel free to e-mail me if you want to discuss this privately.

Nicolas George

unread,
Nov 9, 2012, 6:56:53 PM11/9/12
to
Keith Thompson , dans le message <lnobj6j...@nuthaus.mib.org>, a
�crit�:
> Gordon, I've told you before that you do not have my permission
> to quote anything I write to Usenet without attribution.

Technically, there is an attribution in his message: the References header.

Keith Thompson

unread,
Nov 9, 2012, 10:35:53 PM11/9/12
to
Nicolas George <nicolas$geo...@salle-s.org> writes:
> Keith Thompson , dans le message <lnobj6j...@nuthaus.mib.org>, a
> écrit :
>> Gordon, I've told you before that you do not have my permission
>> to quote anything I write to Usenet without attribution.
>
> Technically, there is an attribution in his message: the References
> header.

I wouldn't call that an attribution. The purpose of an attribution line
is to indicate to the reader who wrote what, and to make the discussion
easier to follow.

luser.droog

unread,
Nov 19, 2012, 1:46:27 AM11/19/12
to
Sounds to me like you need to use strtok() to chop the string into words;
and strcmp() to find out what the word is. Many english words begin
with 'a' followed by not-'a'.

0 new messages