input.txt*hi, &_hi, T@ ese*124
_hi again! test_var
Identifiers found:
_hi 2
again 1
ese124 1
hi 1
test_var 1
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;
};
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 == '_');
}
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
}
This function reads the file character by character and extracts valid identifiers.