Counting occurence of character in a string

M

Michael McGarry

Hi,

Is there a standard library function that counts the number of
occurences of a character in a string?

Regards,

Michael
 
P

Peter Nilsson

pete said:

If it were, it would probably be implemented along the lines of...

size_t strccnt(const char *s, int c)
{
const unsigned char *us = (const unsigned char *) s;
const unsigned char uc = c;
size_t n = 0;
if (!uc) return 1;
while (*us) if (*us++ == uc) n++;
return n;
}
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

Peter said:
If it were, it would probably be implemented along the lines of...

size_t strccnt(const char *s, int c)
{
const unsigned char *us = (const unsigned char *) s;
const unsigned char uc = c;
size_t n = 0;
if (!uc) return 1;

return 1 ?
while (*us) if (*us++ == uc) n++;
return n;
}

What's wrong with a simpler version?
size_t strccnt(const char* s, int c)
{
size_t n = 0;

while(*s != '\0') {
if(*s++ == c)
n++;
}

return n;
}


Bjørn
 
S

Skarmander

Peter said:
If it were, it would probably be implemented along the lines of...

size_t strccnt(const char *s, int c)
{
const unsigned char *us = (const unsigned char *) s;
const unsigned char uc = c;
size_t n = 0;
if (!uc) return 1;
<snip>

I'd argue for "return 0", or rather, omitting the check altogether. The
NUL character does not occur in the string. That would be more something
for a "memccnt" function.

S.
 
J

Jordan Abel

<snip>

I'd argue for "return 0", or rather, omitting the check altogether. The
NUL character does not occur in the string.

Traditionally, the NUL character appears exactly once, at the end of the
string, and this location is returned in strchr/strrchr.
 
P

pete

Jordan said:
Traditionally,
the NUL character appears exactly once, at the end of the
string, and this location is returned in strchr/strrchr.

N869
7. Library
7.1 Introduction
7.1.1 Definitions of terms
[#1] A string is a contiguous sequence of characters
terminated by and including the first null character.
 
S

Skarmander

Jordan said:
Traditionally, the NUL character appears exactly once, at the end of the
string, and this location is returned in strchr/strrchr.
You're right. 7.1.1. "A string is a contiguous sequence of characters
terminated by and including the first null character." This means that a
string is considered to include NUL except where stated otherwise. 1 is
the correct answer. (But I'd make it explicit in the function's
description.)

S.
 
R

Randy Howard

Bjørn Augestad wrote
(in article said:
What's wrong with a simpler version?
size_t strccnt(const char* s, int c)

Apart from namespace problems? Not much. :)
 
D

David Resnick

Randy said:
Bjørn Augestad wrote


Apart from namespace problems? Not much. :)

If you read upthread, Peter Nilsson had posited
his function (with the name strccnt) as how the implementation
would name/code such a function if it were in the standard
library. So in that context the name is fine :)

-David
 
K

Keith Thompson

Skarmander said:
You're right. 7.1.1. "A string is a contiguous sequence of characters
terminated by and including the first null character." This means that
a string is considered to include NUL except where stated otherwise. 1
is the correct answer. (But I'd make it explicit in the function's
description.)

Yes, just as it's explicit in the standard's description of strchr()
and strrchr().
 
O

Old Wolf

Bjørn Augestad said:
return 1 ?

1 occurrence of 0 in the string.
What's wrong with a simpler version?
size_t strccnt(const char* s, int c)
{
size_t n = 0;
while(*s != '\0') {
if(*s++ == c)
n++;
}
return n;
}

Your version works for CHAR_MIN <= c <= CHAR_MAX.
Peter's version works for CHAR_MIN <= c <= UCHAR_MAX.

So his version could safely be called like this:
int ch = getchar();
strccnt(s, ch);
as well as
strccnt(s, 'é');

The functions in <ctype.h> and <stdio.h> expect a value
in the range 0 <= c <= UCHAR_MAX.
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

Old said:
1 occurrence of 0 in the string.

First I read that as if(!us), not if(!uc) and was wondering why the
function returned 1 when it received a NULL pointer. My mistake.
Your version works for CHAR_MIN <= c <= CHAR_MAX.
Peter's version works for CHAR_MIN <= c <= UCHAR_MAX.

So his version could safely be called like this:
int ch = getchar();
strccnt(s, ch);
as well as
strccnt(s, 'é');

The functions in <ctype.h> and <stdio.h> expect a value
in the range 0 <= c <= UCHAR_MAX.

You,re right, thanks. (Why can't we all use ASCII only? ;-) )

Bj&oring;rn
 
P

pete

Old said:
1 occurrence of 0 in the string.


Your version works for CHAR_MIN <= c <= CHAR_MAX.

It doesn't work when c equals zero.
Peter's version works for CHAR_MIN <= c <= UCHAR_MAX.

I don't see the point in searching for values
outside the range of char, in an array of char,
and neither does strchr.
I think it's more better to compare *s against
the value of c converted to type char,
as in the standard's description of strchr.

size_t str_ccnt(const char* s, int c)
{
size_t n = 0;

do {
if (*s == (char)c) {
n++;
}
} while(*s++ != '\0');
return n;
}

strcmp and strncmp are the only standard string functions
which are based on unsigned char interpretation of bytes.
All of the others, like strchr, are based on char values.
I think strccnt should be more like strchr, than like strcmp.
 
P

pete

Peter said:
Do you have chapter and verse on that?

Jordan Abel has an intense desire
to find some context in which it is OK
to use reserved identifiers to name his functions.
I don't know why.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top