fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. void analyzeTokens(const char input[]) {
  6. printf("\nTokens Found:\n");
  7.  
  8. for (size_t i = 0; i < strlen(input); ++i) {
  9. char currentChar = input[i];
  10.  
  11. // 1. Skip whitespace
  12. if (isspace((unsigned char)currentChar)) {
  13. continue;
  14. }
  15.  
  16. // 2. Check for potential operator characters
  17. if (currentChar == '+' || currentChar == '-' || currentChar == '*' ||
  18. currentChar == '/' || currentChar == '>' || currentChar == '<' ||
  19. currentChar == '=' || currentChar == '!') {
  20.  
  21. // 3. The Lookahead: Check if the NEXT character is '='
  22. if (i + 1 < strlen(input) && input[i + 1] == '=') {
  23. // It's a two-character operator
  24. printf("OPERATOR: %c=\n", currentChar);
  25. i++; // IMPORTANT: Increment 'i' again to consume the '='
  26. } else {
  27. // It's just a single-character operator
  28. printf("OPERATOR: %c\n", currentChar);
  29. }
  30. }
  31.  
  32. // Note: You would add 'else if' blocks here to handle letters (identifiers)
  33. // and numbers (literals) if expanding the analyzer further.
  34. }
  35. }
  36.  
  37. int main() {
  38. char codeSnippet[1000];
  39.  
  40. printf("Enter a simple C code snippet: ");
  41. fgets(codeSnippet, sizeof(codeSnippet), stdin);
  42.  
  43. analyzeTokens(codeSnippet);
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0s 5320KB
stdin
int a+= b
stdout
Enter a simple C code snippet: 
Tokens Found:
OPERATOR: +=