Advertisement
maxim_shlyahtin

ex_prep

Nov 14th, 2022
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #define N 5
  7. #define M 20
  8.  
  9. void del_char(char *str, char step) { // удаление определенного символа из строки
  10.     for (int i = 0; str[i]; i++) {
  11.         if (str[i] == step) {
  12.             memmove(&str[i], &str[i + 1], strlen(str) - i);
  13.             i--;
  14.         } else
  15.             continue;
  16.     }
  17. }
  18.  
  19. void del_str(char *str, char *step) { // удаление подстроки из строки
  20.     int len = strlen(step);
  21.     int counter = 0, first_index;
  22.     for (int i = 0; str[i]; i++) {
  23.         if (str[i] == step[counter]) {
  24.             counter++;
  25.             if (counter == 1)
  26.                 first_index = i;
  27.         }
  28.         if (counter == len) {
  29.             counter = 0;
  30.             memmove(&str[first_index], &str[i + 1], strlen(str) - i);
  31.         }
  32.     }
  33. }
  34.  
  35. #define del(str, step) _Generic((step),\
  36.     char: del_char,\
  37.     char* : del_str\
  38. )(str, step)
  39.  
  40. char *add_str(char *str, char *add, int pos) { // вставка подстроки в строку
  41.     int len = strlen(add), counter = 0;
  42.     int size = strlen(str);
  43.     char *new_str = malloc((size + len) * sizeof(char));
  44.     for (int i = 0, j = 0; str[i]; j++) {
  45.         if (j < pos || j >= pos + len) {
  46.             new_str[j] = str[i];
  47.             i++;
  48.         } else if (j >= pos && j < pos + len)
  49.             new_str[j] = add[counter++];
  50.     }
  51.     return new_str;
  52. }
  53.  
  54. void del_str_in_arr(char **str_arr, char *target_str, int arr_len) { // удаление строки из массива строк
  55.     for (int i = 0; i < arr_len; i++) {
  56.         if (!strcmp(str_arr[i], target_str)) {
  57.             int j = i;
  58.             while (j < N) {
  59.                 str_arr[j] = str_arr[j + 1];
  60.                 j++;
  61.             }
  62.             break;
  63.         }
  64.     }
  65. }
  66.  
  67.  
  68.  
  69.  
  70. int str_len(const char *str) { // подсчет слов в строке
  71.     int count = 0;
  72.     for (int i = 0; str[i]; i++) {
  73.         if (isspace(str[i]))
  74.             count++;
  75.     }
  76.     return count + 1;
  77. }
  78.  
  79. int main() {
  80.     int max = 0;
  81.     char **str_arr = malloc(N * sizeof(char *));
  82.     for (int i = 0; i < N; i++)
  83.         str_arr[i] = malloc(M * sizeof(char));
  84.     for (int i = 0; i < N; i++) {
  85.         fgets(str_arr[i], M, stdin);
  86.         str_arr[i][strlen(str_arr[i]) - 1] = str_arr[i][strlen(str_arr[i])]; // удаление \n в конце строки
  87.     }
  88.     char *tmp = "d";
  89.     del_str_in_arr(str_arr, tmp, N);
  90.     for (int i = 0; i < N - 1; i++)
  91.         puts(str_arr[i]);
  92.     for(int i = 0; i < N - 1; i++)
  93.         free(str_arr[i]);
  94.     free(str_arr);
  95.     return 0;
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement