Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Counting identifiers
- 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 ('_').
- Input
- 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.
- Output
- The number of identifiers in each line.
- Example
- Input:
- Dato25 has 2 c-ats and 3 _dogs
- op8ax _yu _yu67 great-job ax~no identifier.
- Output:
- 4
- 3
- //code
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <string.h>
- int main (){
- int i=0, n=0;
- char j;
- while((j=fgetc(stdin))!=EOF)
- {
- if(isalpha(j)>0 || j=='_')
- i++;
- while((j=fgetc(stdin)))
- {
- if(isalpha(j)==0 && isdigit(j)==0 && j!='_'&& j!=' ' && j!='\n' && j!=EOF)
- n=1;
- }
- if(j==' '||j==EOF||j=='\n')
- break;
- } //somewhere here is mistake
- if(n>0)//here not sure
- {
- i--;
- n=0;
- }
- if(j==EOF||j=='\n')//this is right
- {
- printf("%d\n",i);
- i=0;
- }
- if(j==EOF)//this means nothing much
- {
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement