Groups | Search | Server Info | Login | Register


Groups > comp.lang.c > #397437

Re: Small challenge: sort names

From Michael S <already5chosen@yahoo.com>
Newsgroups comp.lang.c
Subject Re: Small challenge: sort names
Date 2026-04-09 02:59 +0300
Organization A noiseless patient Spider
Message-ID <20260409025901.00002bc6@yahoo.com> (permalink)
References (2 earlier) <10r5vh2$3mo17$2@dont-email.me> <10r64uq$3pf3r$1@dont-email.me> <YpzBR.6$rGx6.1@fx24.iad> <20260409001030.000006c9@yahoo.com> <bDBBR.139$Np7d.78@fx15.iad>

Show all headers | View raw


On Wed, 08 Apr 2026 23:35:35 GMT
scott@slp53.sl.home (Scott Lurndal) wrote:

> Michael S <already5chosen@yahoo.com> writes:
> >On Wed, 08 Apr 2026 21:04:56 GMT
> >scott@slp53.sl.home (Scott Lurndal) wrote:
> >  
> >> Bart <bc@freeuk.com> writes:  
> >> >On 08/04/2026 17:25, DFS wrote:    
> >> >> On 4/8/2026 4:22 AM, Janis Papanagnou wrote:    
> >>   
> >> >//count characters in a string
> >> >int countchr(char *str, char chr)
> >> >{int
> >> >c=0,cnt=0;while(str[c]!='\0'){if(str[c]==chr){cnt++;}c++;}return
> >> >cnt;}
> >> >
> >> >I couldn't quite figure out what it did; counting characters in a
> >> >string (like the comment says)? But strlen will do that. Then I
> >> >noticed that the extra 'chr' parameter, so maybe it stops at
> >> >'chr'.
> >> >
> >> >But when I tried running it as countchr("ABCDEFGH", 'E'), it
> >> >returned 1, not 4 or 5.
> >> >
> >> >So I refactored it like this:
> >> >
> >> >   int countchr(char *str, char chr) {
> >> >       int c=0, cnt=0;
> >> >       while (str[c]!='\0') {
> >> >           if (str[c] == chr) cnt++;
> >> >           c++;
> >> >       }
> >> >       return cnt;
> >> >    }    
> >> 
> >> Idiomatically, I might have written
> >> 
> >>    size_t
> >>    countchar(const char *str, char match)
> >>    {
> >>        size_t count = 0u;
> >>        for(; *str != '\0'; str++) count += (*str == match);
> >>        return count;
> >>    }
> >> 
> >> 
> >>      
> >
> >I'd change it to 
> >
> >   size_t
> >   countchar(const char *str, char match)
> >   {
> >       size_t count = 0;
> >       for(; *str != 0; str++) count += (*str == match);
> >       return count;
> >   }  
> 
> Personally, I prefer to properly identify integer constants
> with the appropriate type annotation.  I always explicitly
> check against NULL rather than using !ptr, for example. I
> never use an unadorned integer for a character constant,
> rather using either 'c' for printables, the legal escapes
> (e.g. \n) and for any non-printable, the appropriate
> octal escape.
> 
> Perhaps some of this is pedantic.   I worked with a system 
> where the null pointer constant was not the value zero, so
> I'm aware that !ptr may not actually do the right thing in
> such cases.


Which is irrelevant in this specific case.
BTW, I think that (ptr != 0) is guaranteed to work even on systems with
null pointers not being numerically zero. But I am not certain about it.
What I am certain is that on the right side of assignment to pointer 0
always acts the same as NULL.
But that's too irrelevant for our case.

As to being pedantic w.r.t. '\0', may be you acquired this habit
because of C++? If I am not mistaken, in C++ '\0' has type char and due
to that (or because of, which is IMHO, more appropriate wording) can in
some contexts lead to results different from 0, with differences as huge
as call to completely different procedure. It does not happen in C where
we have no misfeature of type based static polymorphism.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Small challenge: sort names DFS <nospam@dfs.com> - 2026-04-07 23:14 -0400
  Re: Small challenge: sort names Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-04-08 10:22 +0200
    Re: Small challenge: sort names DFS <nospam@dfs.com> - 2026-04-08 12:25 -0400
      Re: Small challenge: sort names Bart <bc@freeuk.com> - 2026-04-08 18:57 +0100
        Re: Small challenge: sort names scott@slp53.sl.home (Scott Lurndal) - 2026-04-08 21:04 +0000
          Re: Small challenge: sort names Michael S <already5chosen@yahoo.com> - 2026-04-09 00:10 +0300
            Re: Small challenge: sort names scott@slp53.sl.home (Scott Lurndal) - 2026-04-08 23:35 +0000
              Re: Small challenge: sort names Michael S <already5chosen@yahoo.com> - 2026-04-09 02:59 +0300
                Re: Small challenge: sort names James Kuyper <jameskuyper@alumni.caltech.edu> - 2026-04-09 06:29 -0400
                Re: Small challenge: sort names cross@spitfire.i.gajendra.net (Dan Cross) - 2026-04-09 15:15 +0000
                Re: Small challenge: sort names Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-04-11 08:49 -0700
                Re: Small challenge: sort names cross@spitfire.i.gajendra.net (Dan Cross) - 2026-04-11 19:59 +0000
                Re: Small challenge: sort names Bonita Montero <Bonita.Montero@gmail.com> - 2026-04-16 18:50 +0200
                Re: Small challenge: sort names cross@spitfire.i.gajendra.net (Dan Cross) - 2026-04-16 19:54 +0000
                Re: Small challenge: sort names Bonita Montero <Bonita.Montero@gmail.com> - 2026-04-17 16:22 +0200
              Re: Small challenge: sort names Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-04-08 17:27 -0700
              Re: Small challenge: sort names Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-04-08 22:14 -0700
                Re: Small challenge: sort names Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-04-09 02:19 -0700
                Re: Small challenge: sort names scott@slp53.sl.home (Scott Lurndal) - 2026-04-09 15:06 +0000
                Re: Small challenge: sort names Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-04-12 06:35 -0700
                Re: Small challenge: sort names scott@slp53.sl.home (Scott Lurndal) - 2026-04-12 14:51 +0000
                Re: Small challenge: sort names Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-04-13 06:31 -0700
                Re: Small challenge: sort names Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-04-12 06:48 -0700
                Re: Small challenge: sort names Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-04-12 16:15 -0700
          Re: Small challenge: sort names Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-04-11 08:44 +0200
        Re: Small challenge: sort names DFS <nospam@dfs.com> - 2026-04-08 17:13 -0400
          Re: Small challenge: sort names Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-04-11 08:59 +0200
            Re: Small challenge: sort names Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-04-11 09:34 +0200
      Re: Small challenge: sort names Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-04-11 08:31 +0200
    Re: Small challenge: sort names BGB <cr88192@gmail.com> - 2026-04-12 13:08 -0500
    What is the point of anything? (Was: Small challenge: sort names) gazelle@shell.xmission.com (Kenny McCormack) - 2026-04-17 20:44 +0000
  Re: Small challenge: sort names Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-04-08 18:46 -0700
  Re: Small challenge: sort names Sort <Sort@invalid.invalid> - 2026-04-11 19:15 +0100
  Re: Small challenge: sort names Bonita Montero <Bonita.Montero@gmail.com> - 2026-04-16 18:24 +0200
    Re: Small challenge: sort names Bart <bc@freeuk.com> - 2026-04-16 18:38 +0100
      Re: Small challenge: sort names Bonita Montero <Bonita.Montero@gmail.com> - 2026-04-16 19:43 +0200
    Re: Small challenge: sort names Bart <bc@freeuk.com> - 2026-04-18 11:22 +0100
      Re: Small challenge: sort names Bonita Montero <Bonita.Montero@gmail.com> - 2026-04-18 17:48 +0200
        Re: Small challenge: sort names Bonita Montero <Bonita.Montero@gmail.com> - 2026-04-19 08:44 +0200
    Re: Small challenge: sort names DFS <nospam@dfs.com> - 2026-04-22 05:33 -0400
      Re: Small challenge: sort names Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-04-22 03:16 -0700
        Re: Small challenge: sort names DFS <nospam@dfs.com> - 2026-04-22 07:35 -0400
          Re: Small challenge: sort names Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-04-23 08:11 -0700
            Re: Small challenge: sort names Bonita Montero <Bonita.Montero@gmail.com> - 2026-04-23 20:14 +0200
              Re: Small challenge: sort names gazelle@shell.xmission.com (Kenny McCormack) - 2026-04-23 18:30 +0000
        Re: Small challenge: sort names gazelle@shell.xmission.com (Kenny McCormack) - 2026-04-23 17:19 +0000
      Re: Small challenge: sort names Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-04-22 14:11 +0200
        Re: Small challenge: sort names James Kuyper <jameskuyper@alumni.caltech.edu> - 2026-04-22 21:27 -0400
          [OT][meta] Gender (was Re: Small challenge: sort names) Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-04-23 15:51 +0200
            Re: [OT][meta] Gender (was Re: Small challenge: sort names) scott@slp53.sl.home (Scott Lurndal) - 2026-04-23 14:45 +0000
              Re: [OT][meta] Gender (was Re: Small challenge: sort names) Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-04-23 17:03 +0200
              Re: [OT][meta] Gender (was Re: Small challenge: sort names) DFS <nospam@dfs.com> - 2026-04-23 20:31 -0400
                Re: [OT][meta] Gender (was Re: Small challenge: sort names) Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-04-23 18:43 -0700
                Re: [OT][meta] Gender (was Re: Small challenge: sort names) gazelle@shell.xmission.com (Kenny McCormack) - 2026-04-24 16:58 +0000
                Re: [OT][meta] Gender (was Re: Small challenge: sort names) Jan van den Broek <balglaas@dds.nl> - 2026-04-24 06:57 +0000
                Re: [OT][meta] Gender (was Re: Small challenge: sort names) Bonita Montero <Bonita.Montero@gmail.com> - 2026-04-24 10:10 +0200
                Re: [OT][meta] Gender (was Re: Small challenge: sort names) David Brown <david.brown@hesbynett.no> - 2026-04-24 12:52 +0200
            Re: [OT][meta] Gender (was Re: Small challenge: sort names) Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-04-23 16:36 -0700
              Re: [OT][meta] Gender (was Re: Small challenge: sort names) Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-04-24 03:01 +0200
                Re: [OT][meta] Gender (was Re: Small challenge: sort names) Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-04-23 18:50 -0700
                Re: [OT][meta] Gender (was Re: Small challenge: sort names) "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-04-24 20:35 -0700
            Re: [OT][meta] Gender (was Re: Small challenge: sort names) Michael S <already5chosen@yahoo.com> - 2026-04-24 14:20 +0300
              Re: [OT][meta] Gender (was Re: Small challenge: sort names) Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-04-24 14:32 +0200
      Re: Small challenge: sort names Bonita Montero <Bonita.Montero@gmail.com> - 2026-04-23 20:10 +0200
        Re: Small challenge: sort names DFS <nospam@dfs.com> - 2026-04-25 10:26 -0400
  Re: Small challenge: sort names Rosario19 <Ros@invalid.invalid> - 2026-04-18 11:16 +0200
    Re: Small challenge: sort names DFS <nospam@dfs.com> - 2026-04-18 08:11 -0400
      Re: Small challenge: sort names Bart <bc@freeuk.com> - 2026-04-18 14:03 +0100
    Re: Small challenge: sort names Rosario19 <Ros@invalid.invalid> - 2026-04-20 21:20 +0200
      Re: Small challenge: sort names DFS <nospam@dfs.com> - 2026-04-20 16:43 -0400
        Re: Small challenge: sort names Rosario19 <Ros@invalid.invalid> - 2026-04-21 05:09 +0200
          Re: Small challenge: sort names DFS <nospam@dfs.com> - 2026-04-21 08:46 -0400
            Re: Small challenge: sort names Bart <bc@freeuk.com> - 2026-04-21 16:08 +0100
              Re: Small challenge: sort names DFS <nospam@dfs.com> - 2026-04-21 11:33 -0400
                Re: Small challenge: sort names Bart <bc@freeuk.com> - 2026-04-21 18:25 +0100
                Re: Small challenge: sort names scott@slp53.sl.home (Scott Lurndal) - 2026-04-21 17:38 +0000
                Re: Small challenge: sort names Michael S <already5chosen@yahoo.com> - 2026-04-21 20:50 +0300
                Re: Small challenge: sort names scott@slp53.sl.home (Scott Lurndal) - 2026-04-21 18:22 +0000
                Re: Small challenge: sort names Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-04-22 03:48 +0200
                Re: Small challenge: sort names David Brown <david.brown@hesbynett.no> - 2026-04-22 09:18 +0200
                Re: Small challenge: sort names Michael S <already5chosen@yahoo.com> - 2026-04-22 13:22 +0300
                Re: Small challenge: sort names David Brown <david.brown@hesbynett.no> - 2026-04-22 12:38 +0200
                Re: Small challenge: sort names Michael S <already5chosen@yahoo.com> - 2026-04-22 13:58 +0300
                Re: Small challenge: sort names David Brown <david.brown@hesbynett.no> - 2026-04-22 13:13 +0200
                Re: Small challenge: sort names Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-04-22 14:53 +0200
                Re: Small challenge: sort names DFS <nospam@dfs.com> - 2026-04-21 16:31 -0400
                Re: Small challenge: sort names Bart <bc@freeuk.com> - 2026-04-21 23:04 +0100
      Re: Small challenge: sort names Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-04-20 14:16 -0700
        An audience of one (Was: Small challenge: sort names) gazelle@shell.xmission.com (Kenny McCormack) - 2026-04-21 13:00 +0000
          Re: An audience of one (Was: Small challenge: sort names) "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-04-21 12:21 -0700
    Re: Small challenge: sort names Rosario19 <Ros@invalid.invalid> - 2026-04-21 05:00 +0200
      Re: Small challenge: sort names Rosario19 <Ros@invalid.invalid> - 2026-04-21 05:06 +0200
  Re: Small challenge: sort names gggggggggggunit <gunitinug@50cent.com> - 2026-04-18 10:19 +0000
    [OT] Shell approach to Re: Small challenge: sort names Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-04-19 09:35 +0200
      Re: [OT] Shell approach to Re: Small challenge: sort names gazelle@shell.xmission.com (Kenny McCormack) - 2026-04-19 14:18 +0000
        Re: [OT] Shell approach to Re: Small challenge: sort names cross@spitfire.i.gajendra.net (Dan Cross) - 2026-04-19 19:32 +0000
          Re: [OT] Shell approach to Re: Small challenge: sort names Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-04-22 03:34 +0200

csiph-web