|
szovegkezeles2C++.cpp
| |
1 // https://odorpeter.hu
2
3 // Szövegkezelés 2. rész
4
5 #include <iostream>
6 #include <string>
7 #include <windows.h> // SetConsoleCP SetConsolOutputCP
8 #include <algorithm> // reverse
9 #include <cstdlib> // atoi
10
11 using namespace std;
12
13 int main()
14 {
15 setlocale(LC_ALL, "hun");
16 SetConsoleCP(1250);
17 SetConsoleOutputCP(1250);
18 string hatar="";
19 for(int i=0;i<59;i++) hatar+="-";
20 cout << hatar << endl;
21 cout << " 1 2 3 4 5 " << endl;
22 cout << "12345678901234567890123456789012345678901234567890123456789" << endl;
23 cout << hatar << endl;
24 string s = "Most az egér a sztár, mert az egér nagyon kedves és aranyos, az egér az egér!";
25 cout << s << endl;
26 int hossz=s.length(); // s.size()
27
28 // szövegrészlet cseréje
29 string keres, csere;
30 cout << "\nMit cseréljek? ";
31 cin >> keres;
32 cout << "Mire cseréljem? ";
33 cin >> csere;
34
35 cout << hatar << endl;
36
37 cout << "\nKeresett szövegrész: " << keres << endl;
38 int poz=s.find(keres);
39 if(poz>-1)
40 {
41 cout << "Első találat a(z) " << (poz+1) << ". karaktertől" << endl;
42 }
43 else
44 {
45 cout << "Ilyen részlet nincs a szövegben" << endl;
46 }
47
48 string s2 = s;
49 string s3=s;
50
51 poz=s2.find(keres);
52 int abszolut_poz=poz;
53 int talalat=0;
54 while(poz>-1)
55 {
56 talalat++;
57 s3=s3.replace(abszolut_poz,keres.length(),csere);
58 s2=s2.substr(poz+keres.length(),s2.length());
59
60 poz=s2.find(keres);
61 abszolut_poz+=poz+csere.length();
62
63 }
64
65 cout << "A keresett részlet " << talalat << " helyen szerepel a szövegben" << endl;
66
67 cout << hatar << endl;
68 cout << "\nSzövegrészlet cseréje" << endl;
69 cout << "Cseréljük a(z) " << keres << " részletet a(z) " << csere << " szóra!" << endl;
70
71 cout << "Az új szöveg: \n" << s3 << endl;
72 return 0;
73 }