Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstring>
- using namespace std;
- struct node
- {
- char data;
- node* next = NULL;
- };
- void create_list(node*& top, int n, char* S) //создание листа способом стэка
- {
- int i;
- node* p;
- top = new node;
- top = NULL;
- for (i = 0; i < n; i++)
- {
- p = new node;
- p->data = S[i];
- p->next = top;
- top = p;
- }
- }
- void show_list(node* top) //показать список
- {
- node* p;
- p = top;
- while (p)
- {
- cout << p->data;
- p = p->next;
- }
- cout << endl;
- }
- int find(node*& top, char* v2)
- {
- int i = 0;
- node* p = top;
- bool t = false;
- int numb;
- int f = strlen(v2);
- while ((p->next != NULL) && !t)
- {
- numb = i;
- if ((v2[i] == p->data) && (p->data != ' ') && i < f)
- {
- i++;
- p = p->next;
- }
- else if ((p->data == ' ') && (i + 1 == f))
- {
- t = true;
- return numb;
- }
- p = p->next;
- }
- return 0;
- }
- void zamena(char* v1, char* v2, node*& top)
- {
- int i;
- bool t = false;//слово не заменено
- node* p=new node;
- p->next = top->next;
- int len = strlen(v1);
- node* p1;
- int address = find(top, v2);
- for (i = 0; i < address; i++)
- {
- top = top->next;
- }
- i = 0;
- while ((top->data != ' ') && i != len)
- {
- if (top->data == ' ')
- {
- p1 = new node;
- p1->data = v1[i];
- p1->next = top;
- top = p->next;
- i++;
- }
- else if (i < len)
- {
- while ((top->next->data!=' ')&&top->next!=NULL)
- top->next = top->next->next;
- }
- else
- {
- top->data = v1[i];
- i++;
- top = top->next;
- }
- }
- top->next = p->next;
- }
- int main()
- {
- setlocale(LC_ALL, "rus");
- node* text;
- char S[128] = ""; // для сохранения строки
- cout << "Введите текст латиницей (не больше 128 символов):\n";
- cin.getline(S, 128);
- int n;
- int i;
- n=strlen(S);
- create_list(text, n, S);
- cout << "Text t: ";
- show_list(text);
- cout << endl;
- char v1[128];
- cout << "Введите первое слово:\n";
- cin.getline(v1, 128);
- char v2[128];
- cout << "Введите второе слово \n";
- cin.getline(v2, 128);
- zamena(v1, v2, text);
- show_list(text);
- return 0;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement