-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_substring_in_main_string.cpp
More file actions
63 lines (50 loc) · 1.59 KB
/
Copy pathreplace_substring_in_main_string.cpp
File metadata and controls
63 lines (50 loc) · 1.59 KB
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
#include <iostream>
#include <string>
using namespace std;
// Function to replace a sub-string in the main string
string replaceSubstring(string mainStr, int startIndex, int length, string replacementStr)
{
// Check if the startIndex is valid
if (startIndex < 0 || startIndex >= mainStr.length())
{
cout << "Invalid start index!";
return mainStr;
}
// Check if the length is valid
if (length < 0 || startIndex + length > mainStr.length())
{
cout << "Invalid length!";
return mainStr;
}
// Create a new string to store the result
string resultStr = "";
// Append the characters before the sub-string to be replaced
for (int i = 0; i < startIndex; ++i)
{
resultStr += mainStr[i];
}
// Append the replacement substring
resultStr += replacementStr;
// Append the characters after the sub-string to be replaced
for (int i = startIndex + length; i < mainStr.length(); ++i)
{
resultStr += mainStr[i];
}
return resultStr;
}
int main()
{
string mainStr, replacementStr;
int startIndex, length;
cout << "Enter the main string: ";
cin >> mainStr;
cout << "Enter the start index of the sub-string to replace (0-based index): ";
cin >> startIndex;
cout << "Enter the length of the sub-string to replace: ";
cin >> length;
cout << "Enter the replacement sub-string: ";
cin >> replacementStr;
string modifiedStr = replaceSubstring(mainStr, startIndex, length, replacementStr);
cout << "Modified string: " << modifiedStr << endl;
return 0;
}