fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. void tokenize(const char *input)
  6. {
  7. int i = 0;
  8. int len = strlen(input);
  9.  
  10. printf("\nTokens Found:\n");
  11.  
  12. while (i < len)
  13. {
  14. // Skip whitespace
  15. if (isspace(input[i]))
  16. {
  17. i++;
  18. continue;
  19. }
  20.  
  21. // Check for two-character operators
  22. if (i + 1 < len)
  23. {
  24. char two[3] = {input[i], input[i + 1], '\0'};
  25.  
  26. if (strcmp(two, "+=") == 0 || strcmp(two, "-=") == 0 ||
  27. strcmp(two, "*=") == 0 || strcmp(two, "/=") == 0 ||
  28. strcmp(two, ">=") == 0 || strcmp(two, "<=") == 0 ||
  29. strcmp(two, "==") == 0 || strcmp(two, "!=") == 0)
  30. {
  31.  
  32. printf("OPERATOR: %s\n", two);
  33. i += 2;
  34. continue;
  35. }
  36. }
  37.  
  38. // Single-character operators
  39. if (input[i] == '+' || input[i] == '-' || input[i] == '*' ||
  40. input[i] == '/' || input[i] == '=' || input[i] == '>' ||
  41. input[i] == '<' || input[i] == '!')
  42. {
  43.  
  44. printf("OPERATOR: %c\n", input[i]);
  45. i++;
  46. continue;
  47. }
  48.  
  49. // Identifiers / keywords
  50. if (isalpha(input[i]) || input[i] == '_')
  51. {
  52. char buffer[256];
  53. int j = 0;
  54. while (i < len && (isalnum(input[i]) || input[i] == '_'))
  55. {
  56. buffer[j++] = input[i++];
  57. }
  58. buffer[j] = '\0';
  59. printf("IDENTIFIER: %s\n", buffer);
  60. continue;
  61. }
  62.  
  63. // Numbers
  64. if (isdigit(input[i]))
  65. {
  66. char buffer[256];
  67. int j = 0;
  68. while (i < len && isdigit(input[i]))
  69. {
  70. buffer[j++] = input[i++];
  71. }
  72. buffer[j] = '\0';
  73. printf("NUMBER: %s\n", buffer);
  74. continue;
  75. }
  76.  
  77. // Unknown character
  78. printf("UNKNOWN: %c\n", input[i]);
  79. i++;
  80. }
  81. }
  82.  
  83. int main()
  84. {
  85. char input[1024];
  86. printf("Enter a simple C code snippet: ");
  87. fgets(input, sizeof(input), stdin);
  88.  
  89. // Remove trailing newline
  90. input[strcspn(input, "\n")] = '\0';
  91.  
  92. tokenize(input);
  93. return 0;
  94. }
Success #stdin #stdout 0.01s 5304KB
stdin
== +=
stdout
Enter a simple C code snippet: 
Tokens Found:
OPERATOR: ==
OPERATOR: +=