There are two small problems. One is that the name is in the
implementation space. it would be better not to call it strxxx where
xxx starts with a lowercase letter. A related point is that the name
is not ideal -- it is really too general.
The second is a little bit more important depending on the use you
plan to make of it. You don't count valid UTF-8 characters, you
simply count UTF-8 "start" bytes (and lone single-byte encodings).
This may not matter, but you should comment the fact at the very
least. The standard mbstowcs function reports an error if the string
contains (in the part it examines) an invalid encoding.
Thanks, strChars() seems a better name
About the second point, don't know if that's the correct way:
#include <stdio.h>
#include <string.h>
size_t strChars(const char *s)
{
size_t i = 0;
if (!s) return 0;
while (*s) {
if ((*s & 0xc0) != 0x80) i++;
s++;
}
return i;
}
int main(void)
{
int i, len;
printf("\nWithout strchars:\n");
printf("%s\n", "Cañaveral");
len = (int)strlen("Cañaveral");
for (i = 0; i < len; i++) putchar('-');
printf("\n");
/*
ouput:
Without strchars:
Cañaveral
----------
^ One more char
*/
printf("\nWith strchars:\n");
printf("%s\n", "Cañaveral");
len = (int)strChars("Cañaveral");
for (i = 0; i < len; i++) putchar('-');
printf("\n");
/*
ouput:
With strchars:
Cañaveral