Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #define N 5
- #define M 20
- void del_char(char *str, char step) { // удаление определенного символа из строки
- for (int i = 0; str[i]; i++) {
- if (str[i] == step) {
- memmove(&str[i], &str[i + 1], strlen(str) - i);
- i--;
- } else
- continue;
- }
- }
- void del_str(char *str, char *step) { // удаление подстроки из строки
- int len = strlen(step);
- int counter = 0, first_index;
- for (int i = 0; str[i]; i++) {
- if (str[i] == step[counter]) {
- counter++;
- if (counter == 1)
- first_index = i;
- }
- if (counter == len) {
- counter = 0;
- memmove(&str[first_index], &str[i + 1], strlen(str) - i);
- }
- }
- }
- #define del(str, step) _Generic((step),\
- char: del_char,\
- char* : del_str\
- )(str, step)
- char *add_str(char *str, char *add, int pos) { // вставка подстроки в строку
- int len = strlen(add), counter = 0;
- int size = strlen(str);
- char *new_str = malloc((size + len) * sizeof(char));
- for (int i = 0, j = 0; str[i]; j++) {
- if (j < pos || j >= pos + len) {
- new_str[j] = str[i];
- i++;
- } else if (j >= pos && j < pos + len)
- new_str[j] = add[counter++];
- }
- return new_str;
- }
- void del_str_in_arr(char **str_arr, char *target_str, int arr_len) { // удаление строки из массива строк
- for (int i = 0; i < arr_len; i++) {
- if (!strcmp(str_arr[i], target_str)) {
- int j = i;
- while (j < N) {
- str_arr[j] = str_arr[j + 1];
- j++;
- }
- break;
- }
- }
- }
- int str_len(const char *str) { // подсчет слов в строке
- int count = 0;
- for (int i = 0; str[i]; i++) {
- if (isspace(str[i]))
- count++;
- }
- return count + 1;
- }
- int main() {
- int max = 0;
- char **str_arr = malloc(N * sizeof(char *));
- for (int i = 0; i < N; i++)
- str_arr[i] = malloc(M * sizeof(char));
- for (int i = 0; i < N; i++) {
- fgets(str_arr[i], M, stdin);
- str_arr[i][strlen(str_arr[i]) - 1] = str_arr[i][strlen(str_arr[i])]; // удаление \n в конце строки
- }
- char *tmp = "d";
- del_str_in_arr(str_arr, tmp, N);
- for (int i = 0; i < N - 1; i++)
- puts(str_arr[i]);
- for(int i = 0; i < N - 1; i++)
- free(str_arr[i]);
- free(str_arr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement