50208. Cyclomatic Complexity

I'm a slow walker, but I never walk backwards.

Task Description

We have a string as a C program; please calculate its cyclomatic complexity. The cyclomatic complexity is the number of keywords, including if, case, while, for, and ? in a program. Note that variables and functions can have names like "for1234" and "while456", but they do not count as the cyclomatic complexity. Also, there will be no string constants having these keywords, and there are no comments to process.

Input Format

The input is a C program.

Output Format

The output is the cyclomatic complexity of the C program.

Sample Description

The purpose of the samples is to calculate the cyclomatic complexity. Both of the samples contain variables or functions with the name similar to the keywords. Note that there could be spaces between the keywords and "(".

Sample Input 1

#include <stdio.h>
void while456(){
    for(int j = 0; j < 2; j++)
        for(int k = 0; k < 2; k++)
            printf("Hello World\n");
int main(){
    int for1234 = 7;
    char while789;
    while789 = 'c';
    for(int i = 0; i < 10; i++)for(int j = 0; j < 10; j++);
    printf("%d\n", for1234);
    printf("%c\n", while789);
    return 0;

Sample Output 1


Sample Input 2

#include <stdio.h>
void if456if(){
    int case999 = 10;
        printf("Greater than 15\n");
    else if (case999 == 15)
        printf("Equal to 15\n");
        printf("Less than 15\n");
void whwhile(){
    int ifcase = 0;
    while (ifcase);
int main(){
    int for123ever = 9;
    printf("%d\n", for123ever);
    int case1 = 5;
    int case2 = 4;
    int A_case=7;
    int case_case = 7;
    int while_while = 7;
    case1>case2 ? case1--:case2++;
    case_case < while_while? case_case++ : while_while--;
    case_case < while_while ? case_case++ : while_while--;
    printf("%d\n", case1);
    printf("%d\n", A_case);
    switch(case1) {
        case 4:
        case 5:
        case 6:
    return 0;

Sample Output 2


Estimated Cyclomatic Number