Advertisement
fallenangel121

Untitled

Jan 14th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6.  
  7. int text_count = 0;
  8.  
  9. int tab_count = 0;
  10. int space_after_count = 0;
  11. int space_before_count = 0;
  12. int capital_letter_count = 0;
  13. int special_symbol_count = 0;
  14. int multiple_spaces_count = 0;
  15.  
  16. int entire_tab_count = 0;
  17. int entire_space_after_count = 0;
  18. int entire_space_before_count = 0;
  19. int entire_capital_letter_count = 0;
  20. int entire_special_symbol_count = 0;
  21. int entire_multiple_spaces_count = 0;
  22.  
  23. char* AddTab(char* text) { // sled vseki abzac izrechenieto zapochva s 1 tabulaciq navutre
  24.  
  25. char* tmpText = (char*)calloc(strlen(text) * 2, sizeof(char)); //*
  26. int i;
  27.  
  28. for (i = 0; i < strlen(text); i++) {
  29.  
  30. if (i == 0 || text[i] == '\n') {
  31. strncat(tmpText, "\t", 1);
  32. entire_tab_count++;
  33. }
  34. strncat(tmpText, &text[i], 1);
  35. }
  36. return tmpText;
  37. }
  38.  
  39. char* AddSpaceAfter(char* text) { //sled vseki prepinatelen znak funkciqta dobavq po 1 shpaciq
  40.  
  41. char* tmpText = (char*)calloc(strlen(text) * 2, sizeof(char));
  42. int i;
  43.  
  44. for (i = 0; i < strlen(text); i++) {
  45.  
  46. if (text[i] == '.' || text[i] == '?' || text[i] == ',' || text[i] == '!' || text[i] == ';' || text[i] == ':' || text[i] == '"') {
  47.  
  48. strncat(tmpText, &text[i], 1);
  49. strncat(tmpText, " ", 1);
  50.  
  51. entire_space_after_count++;
  52.  
  53. }
  54. else {
  55. strncat(tmpText, &text[i], 1);
  56. }
  57. }
  58. return tmpText;
  59. }
  60.  
  61. char* AddSpaceBefore(char* text) { // funkciqta dobavq shpaciq predi lqvata skoba
  62. char* tmpText = (char*)calloc(strlen(text) * 2, sizeof(char));
  63. int i;
  64.  
  65. for (i = 0; i < strlen(text); i++) {
  66. if (text[i] == '(')
  67. {
  68. strncat(tmpText, " (", 2);
  69. entire_space_before_count++;
  70. continue;
  71. }
  72. strncat(tmpText, &text[i], 1);
  73. }
  74. return tmpText;
  75. }
  76.  
  77. char* SentenceToUpper(char* text) { // vsqko izrechenie zapochva s glavna bukva
  78.  
  79. int isNewSentence = 0;
  80.  
  81. char* tmpText = (char*)calloc(strlen(text) * 2, sizeof(char));
  82. int i;
  83.  
  84. for (i = 0; i < strlen(text); i++) {
  85.  
  86. if (text[i] == '.' || text[i] == '!' || text[i] == '?') {
  87. strncat(tmpText, &text[i], 1);
  88. strncat(tmpText, " ", 1);
  89. isNewSentence = 1;
  90. continue;
  91. }
  92.  
  93. if (isNewSentence == 1) {
  94.  
  95. char temp = toupper(text[i]);
  96.  
  97. strncat(tmpText, &temp, 1);
  98. isNewSentence = 0;
  99.  
  100. entire_capital_letter_count++;
  101. }
  102. else {
  103. strncat(tmpText, &text[i], 1);
  104. }
  105. }
  106. return tmpText;
  107. }
  108.  
  109.  
  110. char* RemoveSpecialChars(char* text) { //vseki simvol sys stoynost po-golqma ot 127 biva premahvan
  111. char* tmpText = (char*)calloc(strlen(text) * 2, sizeof(char));
  112.  
  113. for (int i = 0; i < strlen(text); i++) {
  114. int variab = text[i];
  115.  
  116. if (text[i] > '~' || text[i] < 0) {
  117. entire_special_symbol_count++;
  118. strncat(tmpText, &text[i], 0);
  119. }
  120. else if ((text[i] == '?')){
  121. text[i] == ' ';
  122. }else
  123. {
  124. strncat(tmpText, &text[i], 1);
  125. }
  126. }
  127. return tmpText;
  128. }
  129.  
  130. char* RemoveMultipleSpaces(char* text) { //prazni razstoqniq po-golemi ot 1 shpaciq bivat namaleni do 1
  131. char* tmpText = (char*)calloc(strlen(text) * 2, sizeof(char));
  132. int i;
  133.  
  134. for (i = 0; i < strlen(text) - 1; i++) {
  135.  
  136. if (text[i] == ' ' && text[i + 1] == ' ')
  137. {
  138. entire_multiple_spaces_count++;
  139.  
  140. }
  141. else {
  142. strncat(tmpText, &text[i], 1);
  143. }
  144. }
  145. return tmpText;
  146. }
  147.  
  148.  
  149. int main()
  150. {
  151. while (true) {
  152. char text[2000];
  153.  
  154. printf("Enter text:\n");
  155. fflush(stdin);
  156. fgets(text, 1000, stdin);
  157.  
  158. char* editedText = (char*)malloc(sizeof(char) * 2000);
  159. text_count++;
  160.  
  161. strcpy(editedText, RemoveSpecialChars(text));
  162. strcpy(editedText, SentenceToUpper(editedText));
  163. strcpy(editedText, AddSpaceAfter(editedText));
  164. strcpy(editedText, AddTab(editedText));
  165. strcpy(editedText, AddSpaceBefore(editedText));
  166. strcpy(editedText, RemoveMultipleSpaces(editedText));
  167.  
  168.  
  169. float averageTabs = entire_tab_count / text_count;
  170. float averageSpacesBefore = entire_space_before_count / text_count;
  171. float averageSpacesAfter = entire_space_after_count / text_count;
  172. float averageCapitalLetters = entire_capital_letter_count / text_count;
  173. float averageSpecialSymbols = entire_special_symbol_count / text_count;
  174. float averageMultipleSpaces = entire_multiple_spaces_count / text_count;
  175.  
  176. printf("\nFormatted text:\n%s\n", editedText);
  177. printf("\nAdded tabs: %d (Average: %.2f)\n", entire_tab_count, averageTabs);
  178. printf("Added spaces before char: %d (Average: %.2f)\n", entire_space_before_count, averageSpacesBefore);
  179. printf("Added spaces after char: %d (Average: %.2f)\n", entire_space_after_count, averageSpacesAfter);
  180. printf("Changed letters to upper case: %d (Average: %.2f)\n", entire_capital_letter_count, averageCapitalLetters);
  181. printf("Removed special symbols: %d (Average: %.2f)\n", entire_special_symbol_count, averageSpecialSymbols);
  182. printf("Removed multiple spaces: %d (Average: %.2f)\n", entire_multiple_spaces_count, averageMultipleSpaces);
  183.  
  184. }
  185. return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement