Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #define MAXLENGTH 100
- enum STATES {
- BEGIN,
- ERROR,
- DOT,
- CAPITAL_BEGIN,
- CAPITAL_END,
- SMALL_BEGIN,
- SMALL_END,
- WHITE,
- END
- };
- void stringInsert(char dst[][MAXLENGTH], char *src, int *size ){
- int i;
- int a;
- int enable = 1;
- for (i = 0; i <= *size; i++){
- a = strcmp(dst[i],src);
- if(a == 0)
- enable = 0;
- }
- if(enable == 1){
- strcpy(dst[(*size)+1],src);
- (*size)++;
- }
- }
- void stringPrint(char str[][MAXLENGTH], int size){
- int i;
- printf("%d",size);
- for (i = 0; i <= size; i++){
- printf("\n%s",str[i]);
- }
- printf("\n");
- }
- void skener(){
- enum STATES STATE = BEGIN;
- int c,i,j,k,printX,printY;
- int FirstWord = 1;
- int x = 0;
- int y = 1;
- int f_count = 0;
- int l_count = 0;
- int e_count = 0;
- char FIRST[MAXLENGTH][MAXLENGTH];
- char LAST[MAXLENGTH][MAXLENGTH];
- char ELSE[MAXLENGTH][MAXLENGTH];
- char buffer[MAXLENGTH];
- char help_buffer[MAXLENGTH];
- while(1) {
- switch(STATE){
- case BEGIN:
- c = getc(stdin);
- x++;
- if(c == '.'){
- STATE = DOT;
- break;
- }
- else if( isupper(c)){
- STATE = CAPITAL_BEGIN;
- printX = x;
- printY = y;
- i = 0;
- k = 0;
- help_buffer[0] = '\0';
- break;
- }
- else if ( islower(c) ){
- STATE = SMALL_BEGIN;
- printX = x;
- printY = y;
- j = 0;
- k = 0;
- help_buffer[0] = '\0';
- break;
- }
- else if ( c == -1){
- STATE = END;
- break;
- }
- else if ( c == ' ' || c == '\t' || c == '\n'){
- STATE = WHITE;
- break;
- }
- else
- STATE = ERROR;
- break;
- case CAPITAL_BEGIN :
- buffer[i] = c;
- buffer[i+1] = '\0';
- help_buffer[k] = c;
- help_buffer[k+1] = '\0';
- k++;
- i++;
- c = getc(stdin);
- x++;
- if ( islower(c)){
- STATE = CAPITAL_BEGIN;
- break;
- }else{
- STATE = CAPITAL_END;
- break;
- }
- case CAPITAL_END :
- if(FirstWord == 1){
- stringInsert(FIRST, buffer, &f_count );
- }
- if(FirstWord == 0){
- stringInsert(ELSE, buffer, &e_count );
- }
- i = 0;
- printf("\n%s\t_CAPITAL_WORD (%d,%d)",buffer,printX, printY);
- buffer[0] = '\0';
- ungetc(c,stdin);
- x--;
- STATE = BEGIN;
- FirstWord = 0;
- break;
- case SMALL_BEGIN :
- buffer[j] = c;
- buffer[j+1] = '\0';
- j++;
- help_buffer[k] = c;
- help_buffer[k+1] = '\0';
- k++;
- c = getc(stdin);
- x++;
- if ( islower(c)){
- STATE = SMALL_BEGIN;
- break;
- }else{
- STATE = SMALL_END;
- break;
- }
- case SMALL_END :
- if(FirstWord == 1){
- stringInsert(FIRST, buffer, &f_count );
- }
- if(FirstWord == 0){
- stringInsert(ELSE, buffer, &e_count );
- }
- j = 0;
- printf("\n%s\t_WORD (%d,%d)",buffer,printX, printY);
- buffer[0] = '\0';
- ungetc(c,stdin);
- x--;
- STATE = BEGIN;
- FirstWord = 0;
- break;
- case DOT :
- --e_count;
- stringInsert(LAST, help_buffer, &l_count );
- FirstWord = 1;
- printf("\n.\t_DOT\t(%d,%d)",x,y);
- STATE = BEGIN;
- break;
- case ERROR :
- printf("\nGRESKA! ZNAK: %c SE NE NALAZI U RECNIKU\t(%d,%d)",c,x,y);
- STATE = BEGIN;
- break;
- case WHITE:
- if( c == '\n' ){
- x = 0;
- y++;
- }
- STATE = BEGIN;
- break;
- case END :
- printf("\nULAZNE RECI:");
- stringPrint(FIRST,f_count);
- printf("\nLAST RECI:");
- stringPrint(LAST,l_count);
- printf("\nELSE RECI:");
- stringPrint(ELSE,e_count);
- return;
- }
- }
- }
- int main(){
- skener();
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement