Skip to content

Instantly share code, notes, and snippets.

@alfwatt
Created Mar 29, 2018
Embed
What would you like to do?
scountf(char*) - counts the number of replacement strings in a format string, sorta
static int
scountf(const char * format)
{
int count = 0;
int index = 0;
char this, next;
while ((this = format[index])) {
next = format[index++];
if (this == '%' && next != '%') { // skip %%
count++;
}
else if (next == '*') { // surrender
return -1;
}
}
return count;
}
/* Alf Watt - alf@istumbler.net - assigned to the public domain */
@sijanec
Copy link

sijanec commented Mar 14, 2021

If you're using the GNU C library, you can checkout my answer on SO: https://stackoverflow.com/a/66624722/11293716
Or the manual page directly for a built-in solution: https://www.gnu.org/software/libc/manual/html_node/Parsing-a-Template-String.html
And please don't use this gist in production, if you don't want to surrender (;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment