Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. // Created by Темирхан Мамаев on 05/12/2019.
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <limits.h>
  7.  
  8. char *min_alpha(char **s, int size_arr){
  9. char *p_min_alpha = NULL;
  10. int min_count = INT_MAX;
  11. for (char **ps = s; ps < s + size_arr; ps++){
  12. int count = 0;
  13. for (char *sub = *ps; sub < *ps + strlen(*ps); sub++){
  14. if (isalpha(*sub)) {
  15. count++;
  16. }
  17. }
  18. if (count < min_count) {
  19. p_min_alpha = *ps;
  20. min_count = count;
  21. }
  22. }
  23. return p_min_alpha;
  24. }
  25.  
  26. char **podstr_split(char **s, int size_arr, char fs, char ls){
  27. int n = 0;
  28. char **podstr_arr = malloc(sizeof(char*) * 5);
  29. char **ps = podstr_arr;
  30. for(char **str = s; str < s + size_arr; str++){
  31. char *q = *str, *p = *str;
  32. while(isalpha(*q))
  33. if (strchr(q, '*')){
  34. *ps = malloc(sizeof(char) * (p - q));
  35. strncpy(*ps, (q + 1), (p - q - 1));
  36. *(*ps + (p - q)) = '\0';
  37. ps++;
  38. n++;
  39. q++;
  40. if (n % 5 == 0) {
  41. podstr_arr = realloc(podstr_arr, sizeof(char*) * (n + 5));
  42. ps = podstr_arr + n;
  43. }
  44.  
  45. }
  46. }
  47. *(podstr_arr + n) = NULL;
  48. return podstr_arr;
  49. }
  50.  
  51. void vstav_char(char *s, char ps, char h){
  52. char *p = s, *q;
  53. while((p = strchr(p, h))){
  54. q = strchr(p, '\0');
  55. while(q > p){
  56. *(q + 1) = *q;
  57. q--;
  58. }
  59. *(p + 1) = ps;
  60. p++;
  61. }
  62. }
  63.  
  64.  
  65. int main(int argc, const char * argv[]) {
  66. FILE *f;
  67. char buf[80], **p = NULL, **str = NULL, **s, *p_max, **podstr;
  68. int n = 0;
  69. if (!(f = fopen("data.txt", "rb"))) {
  70. printf("file don't exist");
  71. return 1;
  72. }
  73. else{
  74. while (!feof(f)) {
  75. str = (char**)malloc(sizeof(char*) * 5);
  76. p = str;
  77. while (!feof(f)) {
  78. fgets(buf, 79, f);
  79. *p = (char*)malloc((strlen(buf) + 1) * sizeof(char));
  80. strcpy(*p, buf);
  81. p++;
  82. n++;
  83. if (n % 5 == 0) {
  84. str = (char**)realloc(str, sizeof(char*) * (n + 5));
  85. p = str + n;
  86. }
  87. }
  88. }
  89. }
  90. podstr = podstr_split(str, n, '(', ')');
  91. p_max = min_alpha(str, n);
  92. puts(p_max);
  93. for(s = str; s < str + n; s++){
  94. puts(*s);
  95. free(*s);
  96. }
  97. printf("\n__________________________________\n\n");
  98. for(s = podstr; *s; s++){
  99. puts(*s);
  100. free(*s);
  101. }
  102. free(podstr);
  103. free(str);
  104. fclose(f);
  105. printf("\n");
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement