Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Задание 3-2
- // Пользователь вводит в консоли 2 строки: s1 и s2. Напишите программу, удаляющую из
- // строки s1 все символы, которые содержит строка s2. Удаление символов оформить в виде
- // функции.
- #include <stdio.h>
- #include <stdlib.h>
- #define str_maxlen 100
- typedef char* str;
- str str_new() {
- return (str) malloc(sizeof(char) * (str_maxlen + 1));
- }
- str str_read(char end) {
- str s = str_new();
- str c = s;
- while ((*c = getchar()) != end && *c != EOF)
- c++;
- *c = EOF;
- return s;
- }
- str str_nulled(str old) {
- // c89/printf: "%s": The argument shall be a pointer to an array of character type.
- // Characters from the array are written up to (but not including) a terminating null character;
- // if the precision is specified, no more than that many characters are written.
- // If the precision is not specified or is greater than the size of the array, the array shall contain a null character.
- // NOTE: EOF gets printed somehow ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- str s = str_new();
- str c = s;
- while(*old != EOF && *old != '\0')
- *(c++) = *(old++);
- *c = '\0';
- return s;
- }
- int str_index_of(str s, char c) {
- int n = 0;
- while (*s != EOF) {
- n++;
- if (*s == c) return n;
- s++;
- }
- return -1;
- }
- str str_remove_chars(str old, str chars) {
- str s = str_new();
- str c;
- for (c = s; *old != EOF; old++) {
- if (str_index_of(chars, *old) == -1) {
- *(c++) = *old;
- }
- }
- *c = EOF;
- return s;
- }
- int main() {
- printf("s1:\n");
- str s = str_read('\n');
- printf("s2:\n");
- str r = str_read('\n');
- printf("s1 without chars in s2 is \"%s\"\n", str_nulled( str_remove_chars(s, r) ));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement