-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSubstring.cpp
More file actions
35 lines (26 loc) · 992 Bytes
/
Substring.cpp
File metadata and controls
35 lines (26 loc) · 992 Bytes
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
#include <iostream>
#include <string>
using namespace std;
int main() {
string inputString, subString;
int startIndex, length;
// Input: Enter the original string
cout << "Enter the original string: ";
getline(cin, inputString);
// Input: Enter the starting index
cout << "Enter the starting index (0-based): ";
cin >> startIndex;
// Input: Enter the length of the substring
cout << "Enter the length of the substring: ";
cin >> length;
// Validate the input indices
if (startIndex < 0 || startIndex >= inputString.length() || length < 0 || startIndex + length > inputString.length()) {
cout << "Invalid input. Check the starting index and length." << endl;
return 1; // Exit with an error code
}
// Create the substring
subString = inputString.substr(startIndex, length);
// Output: Display the created substring
cout << "Substring: " << subString << endl;
return 0; // Exit successfully
}