Advertisement
Dimava

3-2

Mar 27th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. // Задание 3-2
  2. // Пользователь вводит в консоли 2 строки: s1 и s2. Напишите программу, удаляющую из
  3. // строки s1 все символы, которые содержит строка s2. Удаление символов оформить в виде
  4. // функции.
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. #define str_maxlen 100
  10.  
  11. typedef char* str;
  12.  
  13. str str_new() {
  14.     return (str) malloc(sizeof(char) * (str_maxlen + 1));
  15. }
  16.  
  17. str str_read(char end) {
  18.     str s = str_new();
  19.     str c = s;
  20.     while ((*c = getchar()) != end && *c != EOF)
  21.         c++;
  22.     *c = EOF;
  23.     return s;
  24. }
  25.  
  26. str str_nulled(str old) {
  27. // c89/printf: "%s": The argument shall be a pointer to an array of character type.
  28. //                   Characters from the array are written up to (but not including) a terminating null character;
  29. //                   if the precision is specified, no more than that many characters are written.
  30. //                   If the precision is not specified or is greater than the size of the array, the array shall contain a null character.
  31. // NOTE: EOF gets printed somehow                                                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  32.     str s = str_new();
  33.     str c = s;
  34.     while(*old != EOF && *old != '\0')
  35.         *(c++) = *(old++);
  36.     *c = '\0';
  37.     return s;
  38. }
  39.  
  40. int str_index_of(str s, char c) {
  41.     int n = 0;
  42.     while (*s != EOF) {
  43.         n++;
  44.         if (*s == c) return n;
  45.         s++;
  46.     }
  47.     return -1;
  48. }
  49.  
  50. str str_remove_chars(str old, str chars) {
  51.     str s = str_new();
  52.     str c;
  53.     for (c = s; *old != EOF; old++) {
  54.         if (str_index_of(chars, *old) == -1) {
  55.             *(c++) = *old;
  56.         }
  57.     }
  58.     *c = EOF;
  59.     return s;
  60. }
  61.  
  62. int main() {
  63.     printf("s1:\n");
  64.     str s = str_read('\n');
  65.     printf("s2:\n");
  66.     str r = str_read('\n');
  67.     printf("s1 without chars in s2 is \"%s\"\n", str_nulled(  str_remove_chars(s, r)  ));
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement