ProgLab5

Run Settings
LanguageC
Language Version
Run Command
#include <stdlib.h> #include <stdio.h> #include <malloc.h> //0 or 1 int contains(char* array, int arrsize, char c){ int i; for(i=0; i<arrsize; i++){ if(array[i]==c) return 1; } return 0; } int main(){ char* splitters = malloc(sizeof(char)); int splitters_count=0; char c; while((c = getchar()) != '\n'){ splitters = realloc(splitters, sizeof(char) * (++splitters_count)); splitters[splitters_count-1] = c; } int maxI; scanf("%d", &maxI); getchar(); // for '\n' if(splitters_count==0){ printf(maxI==0?"0":"1"); free(splitters); splitters = NULL; return 0; } char* str = malloc(sizeof(char)*maxI); int i; int isWord = 0; int ptr=0; for(i=0; i<maxI; i++){ c = getchar(); if(isWord){ str[ptr++] = c; if(contains(splitters, splitters_count, c)){ isWord = 0; } }else{ if(!contains(splitters, splitters_count, c)){ str[ptr++] = c; isWord = 1; } } } if(ptr!=0 && contains(splitters, splitters_count, str[ptr-1])){ ptr--; } if(ptr==0){ printf("0"); free(splitters); splitters = NULL; free(str); str = NULL; return 0; } str = (char*) realloc(str, ptr); maxI = ptr; int lastWordLength=0; for(i=maxI-1; i>=0; i--){ if(!contains(splitters, splitters_count, str[i])){ lastWordLength++; }else{ break; } } int wordsCount = 1; int curWordLength=0; for(;i>=0;i--){ if(contains(splitters, splitters_count, str[i])){ if(curWordLength==lastWordLength){ wordsCount++; } curWordLength = 0; }else{ curWordLength++; if(i==0){ } } } if(curWordLength==lastWordLength){ wordsCount++; } printf("%d", wordsCount); free(splitters); splitters = NULL; free(str); str = NULL; return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines