Advertisement
JStefan

[Laboratoriski] Palindromi datoteki

Dec 18th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int e_palindrom(char *);
  6. void removeip(char *);
  7.  
  8. int main()
  9. {
  10.     FILE *fvlez;
  11.     FILE *fizlez;
  12.  
  13.     if((fvlez = fopen("text.txt", "r")) == NULL || (fizlez = fopen("print.txt", "w")) == NULL) {
  14.         printf("Datotekata nemoze da se prochita");
  15.         return -1;
  16.     }
  17.  
  18.     char zbor[15];
  19.     while( (fscanf(fvlez, "%s", zbor)) == 1){
  20.         removeip(zbor);
  21.         if(e_palindrom(zbor)) fprintf(fizlez, "%s\n", zbor);
  22.     }
  23.  
  24.     fclose(fvlez);
  25.     fclose(fizlez);
  26.  
  27.     return 0;
  28. }
  29.  
  30. int e_palindrom(char *zbor) {
  31.     int i = 0, j = strlen(zbor)-1;
  32.     for(; i < j; ++i, --j) { if(tolower(zbor[i]) != tolower(zbor[j])) return 0; }
  33.     return 1;
  34. }
  35.  
  36. void removeip(char *zbor) {
  37.     int i = 0;
  38.     while(zbor[i] != '.' && zbor[i] != ',' && zbor[i] != 0) ++i;
  39.     if(i != strlen(zbor)) zbor[i] = '\0';
  40.     else return;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement