fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. char keyword[32][10]=
  6. {
  7. "auto","double","int","struct","break","else","long",
  8. "switch","case","enum","register","typedef","char",
  9. "extern","return","union","const","float","short",
  10. "unsigned","continue","for","signed","void","default",
  11. "goto","sizeof","voltile","do","if","static","while"
  12. } ;
  13.  
  14. int is_valid(char str[])
  15. {
  16. if (isdigit(str[0]))
  17. return 0;
  18. int i = 0;
  19. while(str[i]!='\0')
  20. {
  21. char ch = str[i];
  22. if(!isdigit(ch) && !isalpha(ch) && ch!='-')
  23. return 0;
  24. i++;
  25. }
  26.  
  27. return 1;
  28. }
  29. int is_keyword(char s[])
  30. {
  31. int i = 0;
  32. for (i = 0 ; i< 32 ; i++)
  33. {
  34. if(strcmp(keyword[i],s)==0)
  35. return 0;
  36. }
  37. return 1;
  38. }
  39. int main()
  40. {
  41.  
  42. char str[100];
  43. char key[100];
  44.  
  45.  
  46. int i,a=0;
  47. int space=0;
  48. printf("Enter a string\n");
  49. scanf("%[^.]s",str);
  50.  
  51.  
  52.  
  53.  
  54. char sp[] = " ";
  55. char words[100][100] , valid_words[100][100] , invalid_words[100][100];
  56. char *ptr = strtok(str, sp);
  57. int ind = 0;
  58.  
  59.  
  60. while(ptr != NULL)
  61. {
  62.  
  63. strcpy(words[ind++], ptr);
  64. ptr = strtok(NULL, sp);
  65.  
  66.  
  67.  
  68. }
  69.  
  70.  
  71.  
  72.  
  73.  
  74. int ind1 = 0, ind2=0;
  75. for(i= 0 ; i<ind ; i++)
  76. {
  77. if(is_keyword(words[i])==1)
  78. if(is_valid(words[i])==1)
  79. {
  80. strcpy(valid_words[ind1++], words[i]);
  81. }
  82. else
  83. {
  84. strcpy(invalid_words[ind2++], words[i]);
  85. }
  86. }
  87.  
  88. printf("\n\nValid Identifiers: ");
  89. for( i= 0; i< ind1 ; i++)
  90. {
  91. if(i!=ind1-1)
  92. {
  93. printf("%s ,",valid_words[i]);
  94. }
  95. else
  96. printf("%s",valid_words[i]);
  97. }
  98.  
  99. printf("\n\nInvalid Identifiers: ");
  100. for( i= 0; i< ind2 ; i++)
  101. {
  102. if(i!=ind2-1)
  103. {
  104. printf("%s ,",invalid_words[i]);
  105. }
  106. else
  107. printf("%s",invalid_words[i]);
  108. }
  109.  
  110.  
  111. printf("\n\nKeywords: \n");
  112. for(i= 0 ; i<ind ; i++)
  113. {
  114. if(is_keyword(words[i])==0)
  115. printf("%s\n", words[i]);
  116. }
  117.  
  118.  
  119. }
  120.  
  121.  
Success #stdin #stdout 0s 5504KB
stdin
CSE 411
Compliler Sessional
Adnan if 
goto
if else.
stdout
Enter a string


Valid Identifiers: CSE

Invalid Identifiers: 411
Compliler ,Sessional
Adnan ,
goto
if

Keywords: 
if
else