int strcnt(char *str, int x) {
int y = 0;
while (*str++) {
if (*str == (x+48)) {
y++;
}
}
return y;
}
void main()
{
char abc[] = "12343212343212";
int n = 2;
printf("%d\n", strcnt(abc, n));
}
Here *str gives the ASCII value. So the int passed has to be converted to an ASCII value. As the ASCII value of '0' is 48, so 48 is added to x and compared with *str
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.