Advertisement
Guest User

Counting identifiers

a guest
Nov 14th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 KB | None | 0 0
  1. Counting identifiers
  2. Write the program that counts how many identifiers are in a given line. Identifier is a sequence of characters from set 'a'-'z' or 'A'-'Z' or '0'-'9' or '_', starting from any letter or underline character ('_').
  3.  
  4. Input
  5. There are given some number of data sets. Each data set is a line consisting from the sequence of some number of words, separated by spaces and finishing with the end of line character (even the last one line). A word is a sequence of any ASCII character of code from 33 till 126 (see http://www.asciitable.com for more details), e.g., aqui28$-3q or _dat_. The second word is an identyfier, but the first one not.
  6.  
  7. Output
  8. The number of identifiers in each line.
  9.  
  10. Example
  11. Input:
  12.  
  13. Dato25 has 2 c-ats and 3 _dogs
  14. op8ax _yu _yu67 great-job ax~no identifier.
  15.  
  16.  
  17. Output:
  18.  
  19. 4
  20. 3
  21.  
  22. //code
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. int main (){
  28.     int i=0, n=0;
  29.     char j;
  30.     while((j=fgetc(stdin))!=EOF)
  31.     {
  32.         if(isalpha(j)>0 || j=='_')
  33.                     i++;
  34.         while((j=fgetc(stdin)))
  35.         {
  36.             if(isalpha(j)==0 && isdigit(j)==0 && j!='_'&& j!=' ' && j!='\n' && j!=EOF)
  37.               n=1;
  38.              
  39.             }
  40.             if(j==' '||j==EOF||j=='\n')
  41.               break;
  42.         } //somewhere here is mistake
  43.               if(n>0)//here not sure
  44.               {
  45.                   i--;
  46.                   n=0;
  47.               }
  48.  
  49.               if(j==EOF||j=='\n')//this is right
  50.               {
  51.                   printf("%d\n",i);
  52.                   i=0;
  53.               }
  54.               if(j==EOF)//this means nothing much
  55.               {
  56.                   break;
  57.               }
  58.       }
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement