Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5.  
  6. # define SIZE 500
  7.  
  8. int strlen(char * str);
  9. int isDelimiter(char c);
  10. void checkRegister(char * buffer);
  11. void showСorrectText(char * buffer, FILE * result);
  12.  
  13. int main() {
  14.    
  15.     int i = 0;
  16.     char buffer[SIZE];
  17.  
  18.     FILE * text, *result;
  19.     text = fopen("text.txt", "r");
  20.     result = fopen("newtext.txt", "w");
  21.  
  22.     if (text == NULL || result == NULL) {
  23.         printf("Error occured!\n");
  24.         system("pause");
  25.         exit(1);
  26.     }
  27.  
  28.     fgets(buffer, SIZE, text);
  29.  
  30.     checkRegister(buffer);
  31.     showСorrectText(buffer, result);
  32.  
  33.     fclose(text);
  34.     fclose(result);
  35.     system("pause");
  36.     return 1;
  37. }
  38.  
  39. int strlen(char * str) {
  40.     int len = 0;
  41.     while (str[len++]);
  42.     return len - 1;
  43. }
  44.  
  45. int isDelimiter(char c) {
  46.     int i;
  47.     char delimiters[] = {
  48.         '.',
  49.         '!',
  50.         '?'
  51.     };
  52.     for (i = 0; i < 3; i++)
  53.         if (c == delimiters[i])
  54.             return 1;
  55.     return 0;
  56. }
  57.  
  58. void checkRegister(char * buffer) {
  59.     for (int j = 0; buffer[j] != '\0'; j++) {
  60.  
  61.         if (isDelimiter(buffer[j])) {
  62.             if (toupper(buffer[j + 1]))
  63.                 buffer[j + 1] = toupper(buffer[j + 1]);
  64.         }
  65.  
  66.         if (isDelimiter(buffer[j]) && (buffer[j + 1] == ' ')) {
  67.             if (toupper(buffer[j + 2])) {
  68.                 buffer[j + 2] = toupper(buffer[j + 2]);
  69.                 j += 2;
  70.             }
  71.         }
  72.         else if (tolower(buffer[j]) && !isDelimiter(buffer[j - 1]))
  73.             buffer[j] = tolower(buffer[j]);
  74.  
  75.         if (toupper(buffer[0]))
  76.             buffer[0] = toupper(buffer[0]);
  77.  
  78.     }
  79. }
  80.  
  81. void showСorrectText(char * buffer, FILE * result) {
  82.     int j, i;
  83.     char temp[SIZE];
  84.  
  85.     for (j = 0, i = 0; buffer[j] != '\0';) {
  86.         if (buffer[i] == ' ' && buffer[i + 1] == ' ') {
  87.             temp[j] = ' ';
  88.             i++;
  89.             continue;
  90.         }
  91.  
  92.         if ((isDelimiter(buffer[i]) || buffer[i] == ',') && (buffer[i + 1] != ' ')) {
  93.             temp[j] = buffer[i];
  94.             temp[j + 1] = ' ';
  95.             j += 2;
  96.             i++;
  97.             continue;
  98.         }
  99.  
  100.         temp[j] = buffer[i];
  101.         i++;
  102.         j++;
  103.     }
  104.     temp[j] = '\0';
  105.  
  106.     for (i = 0; temp[i] != '\0'; i++) {
  107.         printf("%c", temp[i]);
  108.     }
  109.  
  110.     fputs(temp, result);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement