I have the string str
str
char *str = "100.10b.100.100";
I want to count the occurrences of '.' in str, preferably a one-liner. (If possible no loops)
'.'
My approach would be the standard strchr:
strchr
int i = 0; char *pch=strchr(str,'.'); while (pch!=NULL) { i++; pch=strchr(pch+1,'.'); }
Here's the way I'd do it (minimal number of variables needed):
for (i=0; s[i]; s[i]=='.' ? i++ : *s++);
1.4m articles
1.4m replys
5 comments
57.0k users