Skip to content

Instantly share code, notes, and snippets.

@alfwatt
Created March 29, 2018 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfwatt/279b694f702d0d6910cd259ce427dfc3 to your computer and use it in GitHub Desktop.
Save alfwatt/279b694f702d0d6910cd259ce427dfc3 to your computer and use it in GitHub Desktop.
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 */
@alfwatt
Copy link
Author

alfwatt commented Mar 29, 2018

80% of the time it works 100% of the time

@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