Untitled
unknown
plain_text
4 months ago
1.3 kB
2
Indexable
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAX_SIZE 10000 void count_tokens(const char *input) { int count = 0; const char *ptr = input; while (*ptr) { if (isspace(*ptr)) { ptr++; continue; } if (strchr("()[]{};,+-*/&|!<>=.", *ptr)) { count++; ptr++; continue; } while (*ptr && !isspace(*ptr) && !strchr("()[]{};,+-*/&|!<>=.", *ptr)) { ptr++; } count++; } printf("Total number of tokens: %d\n", count); } int main() { FILE *file_ptr; char *content; char ch; size_t index = 0; file_ptr = fopen("tokens.c", "r"); if (file_ptr == NULL) { printf("File can't be opened\n"); return EXIT_FAILURE; } content = malloc(MAX_SIZE * sizeof(char)); if (content == NULL) { printf("Memory allocation failed\n"); fclose(file_ptr); return EXIT_FAILURE; } printf("Content of the file is:\n"); while ((ch = fgetc(file_ptr)) != EOF) { printf("%c", ch); content[index++] = ch; } content[index] = '\0'; fclose(file_ptr); count_tokens(content); free(content); return 0; }
Editor is loading...
Leave a Comment