-
Notifications
You must be signed in to change notification settings - Fork 0
/
notReplaceSubString.cpp
56 lines (46 loc) · 1.42 KB
/
notReplaceSubString.cpp
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
#include <iostream>
#include <string>
using namespace std;
string replaceWith(string main_string, string sub_string);
int main()
{
string main_string, sub_string;
cout << "Enter a string: " << endl;
getline(cin, main_string);
cout << "Enter the substring you want to search: " << endl;
getline(cin, sub_string);
cout << replaceWith(main_string, sub_string) << endl;
return 0;
}
string replaceWith(string main_string, string sub_string)
{
int length_subString = sub_string.size();
int length_mainString = main_string.size();
string temp;
for (int i = 0; i < length_mainString; ++i)
{
if(main_string[i] != ' ')
temp = temp + main_string[i];
else if(main_string[i] == ' ')
{
if(sub_string != temp)
{
int start_poisition = i - temp.size();
int end_poisition = i - 1;
for(int j = start_poisition; j <= end_poisition; j++)
main_string[j] = '+';
temp = "";
}
else if(sub_string == temp)
temp = "";
}
if(i == length_mainString - 1 && sub_string != temp)
{
int start_poisition = i - temp.size() + 1;
int end_poisition = i;
for(int j = start_poisition; j <= end_poisition; j++)
main_string[j] = '+';
}
}
return main_string;
}