cybersecurity · beginner · ~15 min

Count percent signs

Tally format-control characters.

Challenge

Count the % characters in a string — a quick risk indicator for text about to be used as a printf format.

Task

Implement int count_percent(const char *s) that returns how many % characters appear in s.

Input

  • s: a NUL-terminated string the grader passes.

Output

Returns int: the total number of % characters (each one counts, including those in %%).

Example

count_percent("%d and %s")   ->   2
count_percent("100%%")       ->   2   (both percent signs count)
count_percent("clean")       ->   0

Edge cases

  • A string with no % returns 0.
  • Every % is counted, escaped or not.

Rules

  • Simple tally — no format parsing.

Input format

A NUL-terminated string s.

Output format

An int: the count of % characters in s.

Constraints

Count every %, including those in %%.

Starter code

int count_percent(const char *s) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.