Advertisement
Guest User

Untitled

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