How to replace substrings in a string?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 #include <iostream>
#include <iomanip>
#include <cstring>
#include<stdarg.h>
#include<string.h>
using namespace std;



int main()
{
	char str1[] = "abc def ghi jkw";
	char str2[] = "qwe asd zxc bnm";
	char str3[] = "afd qwe tyz opq";


	cout << "PLease enter two substring"; /* str90 & str 91 are substring that use to find the substring in main string  */
									      /*                                                                             */ 
	char str90[30];                       /*                                                                             */ 
	cin.get(str90, 30);                   /*                                                                             */ 
	char str91[30];                       /*                                                                             */ 
	cin.get(str91, 30);                   /*                                                                             */ 



	if (strstr(str1, str90) && strstr(str1, str91))
	
		//to replace
		{
			cout << "Please enter the first string that you want to replace.";
			char str101[30];                    // str101 is the substring tht we want to replace //
			cin.get(str101, 30);                             // str101 &str202 should be the first 2substring in the string //

			cout << "Please enter the second string tht u want to replace.";
			char str201[30];
			cin.get(str201, 30);



			string str = str1;
			str.replace(str.begin() +8 , str.end() - 8, 10, str101);          
			str.replace(str.begin() + 12, str.end() - 4, 10, str201);
		}
	

		if (strstr(str2, str90) && strstr(str2, str91))
		
			//to replace
			{
				cout << "Please enter the first string that you want to replace.";
				char str102[30];                    // str102 is the substring tht we want to replace //
				cin.get(str102, 30);                             // str102 &str203 should be the first 2substring in the string //

				cout << "Please enter the second string tht u want to replace.";
				char str202[30];
				cin.get(str202, 30);



				string str = str2;
				str.replace(str.begin() + 8, str.end() - 8, 10, str102);
				str.replace(str.begin() + 12, str.end() - 4, 10, str202);
			}
	
			if (strstr(str3, str90) && strstr(str3, str91))
			
				//to replace
				{
					cout << "Please enter the first string that you want to replace.";
					char str103[30];                    // str103 is the substring tht we want to replace //
					cin.get(str103, 30);                             // str103 &str203 should be the first 2substring in the string //

					cout << "Please enter the second string tht u want to replace.";
					char str203[30];
					cin.get(str203, 30);



					string str = str3;
					str.replace(str.begin() + 8, str.end() - 8, 10, str103);
					str.replace(str.begin() + 12, str.end() - 4, 10, str203);
				}




	// for example if we keyin 'abc' and 'def', str1 is to be edited.
				// Thus, we enter 2 substring for example: 'erf' and 'wer'.
				// Then str1 will be edited and become "abc def erf wer". 
Last edited on
closed account (48T7M4Gy)
So what's the plan here? Do we keep opening up threads on basically the same topic because you aren't prepared to learn by using the tutorials here?

Which part of the previous advice don't you get that needs 89 lines of your voluminous and convoluted code. Can't you just show a few lines to describe what the problem is? After all, the tutorial you've been shown covers this admirably. :)

http://www.cplusplus.com/forum/beginner/195527/
Last edited on
Topic archived. No new replies allowed.