Sample Input File: input.txt

*hi, &_hi,   T@  ese*124
_hi again! test_var

Sample Output

Identifiers found:
_hi 2
again 1
ese124 1
hi 1
test_var 1

Step 1: Define the Struct

First, create a struct to hold each identifier and its frequency count.

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

struct Identifier {
    char name[100];
    int count;
};

Step 2: Create Helper Functions for Character Checking

These functions check if a character can be part of an identifier.

// Check if character can START an identifier (letter or underscore)
int isValidIdentifierStart(char c) {
    return (c >= 'A' && c <= 'Z') || 
           (c >= 'a' && c <= 'z') || 
           (c == '_');
}

// Check if character can be IN an identifier (letter, digit, or underscore)
int isValidIdentifierChar(char c) {
    return (c >= 'A' && c <= 'Z') || 
           (c >= 'a' && c <= 'z') || 
           (c >= '0' && c <= '9') || 
           (c == '_');
}

Step 3: Function to Find if Identifier Already Exists

This function searches the array to see if we've already found this identifier.

// Returns index if found, -1 if not found
int findIdentifier(struct Identifier* ids, int count, char* name) {
    for (int i = 0; i < count; i++) {
        if (strcmp(ids[i].name, name) == 0) {
            return i;  // Found it!
        }
    }
    return -1;  // Not found
}

Step 4: Function to Extract Identifiers from File

This function reads the file character by character and extracts valid identifiers.