In article <94e58990-7dba-406b-8845-71123b4caf07@googlegroups.com>,
sinbad <sinbad.sin...@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 ***
Barry Margolin <bar...@alum.mit.edu> writes:
> In article <94e58990-7dba-406b-8845-71123b4caf07@googlegroups.com>,
> sinbad <sinbad.sin...@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.
sinbad <sinbad.sin...@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"
Keith Thompson <ks...@mib.org> writes:
>sinbad <sinbad.sin...@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.
> >> 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
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.
>>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.
On 2012-10-31, Keith Thompson <ks...@mib.org> wrote:
> sinbad <sinbad.sin...@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.
>> >> 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
>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?
Nathan Wagner <n...@hydaspes.if.org> writes:
> On 2012-10-31, Keith Thompson <ks...@mib.org> wrote:
>> sinbad <sinbad.sin...@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 (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"
Barry Schwarz <schwa...@dqel.com> writes:
> On Thu, 1 Nov 2012 08:09:00 -0700 (PDT), sinbad
> <sinbad.sin...@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.sin...@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.
-- 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"
On Thu, 01 Nov 2012 15:59:21 -0700, Keith Thompson <ks...@mib.org>
wrote:
>Barry Schwarz <schwa...@dqel.com> writes:
>> On Thu, 1 Nov 2012 08:09:00 -0700 (PDT), sinbad
>> <sinbad.sin...@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.
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.sin...@gmail.com> wrote:
>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.
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.
> 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.
-- 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"
> 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).
gordonb.eo...@burditt.org (Gordon Burditt) writes:
> Keith Thompson <ks...@mib.org> writes: [attribution line added]
>> 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).
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.
-- 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"
Nicolas George <nicolas$geo...@salle-s.org> writes:
> Keith Thompson , dans le message <lnobj6jynh....@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.
-- 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"
sinbad wrote:
> 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.
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'.