cybersecurity · beginner · ~15 min · safe pentest lab

Count scanned hosts

Count elements in XML output.

Challenge

Summarise scan coverage by counting how many hosts a saved nmap XML report contains.

Task

Implement int count_hosts(const char *xml) that returns the number of <host opening tags in xml.

Input

  • xml: a NUL-terminated string holding saved nmap XML, baked into the harness.

Output

Returns int: the count of <host substrings.

Example

count_hosts("<host>a</host><host>b</host>")   ->   2
count_hosts("<ports/>")                        ->   0

Edge cases

  • No host elements returns 0.

Rules

  • Pure string scan on a static buffer — never a live scan.

Input format

A NUL-terminated string xml of saved nmap output.

Output format

An int: the number of <host opening tags.

Constraints

Count the substring <host. Static buffer only.

Starter code

#include <string.h>

int count_hosts(const char *xml) {
    /* TODO */
    return 0;
}

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