Count occurrences of a substring Dry Run in C

Count occurrences of a substring is an interactive C dry run visualizer from the String Functions programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Count occurrences of a substring Program Code

#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "ababcababcab";
    char substr[] = "ab";
    int count = 0;
    int len_str = strlen(str);
    int len_sub = strlen(substr);
    int i, j, match;
    
    printf("String: %s\n", str);
    printf("Substring: %s\n", substr);
    
    for(i = 0; i <= len_str - len_sub; i++) {
        match = 1;
        for(j = 0; j < len_sub; j++) {
            if(str[i + j] != substr[j]) {
                match = 0;
                break;
            }
        }
        if(match) {
            count++;
            i += len_sub - 1; // Skip overlapping (optional)
        }
    }
    
    printf("Occurrences: %d\n", count);
    return 0;
}

View the complete String Functions programs page.

Program Console Count occurrences of a substring Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.