I have difficulty in understanding you logic, and therefore jump into some of my own intepretation.
- I make
n
the number of strings to be input.
- Each string use
str.size()
to get the length for testing.
- Change you '1' to '<' and '2' to '>', thinking that you are finding the length between
<..>
.
- move the output inside the while loop
n
, simply for easies to observe, you may bring this line back to the end of the program. It won'e change the execution.
The code I changed to:
#include<iostream>
#include<string>
using namespace std;
int main() {
int n = 0, i =0, count = 0, maxi = 0, index = 0;
string str;
char bra='<', key='>'; // change the delimiters
cout << "Enter test runs : n = ";
cin >> n; // assume n the number of test runs
while (n--)
{
cout << "Enter the brackets : ";
cin >> str;
for (i = 0; i < str.size(); i++) { //use str.size();
if (str[i] == bra) {
count = 0;
while (str[++i] != key) { count++; }
if (count > maxi) {
maxi = count;
index = i - count;
}
}
}
cout << "depth= " << maxi << endl << "starting index = " << index <<std::endl;
}
return 0;
}
A test:
$ ./a.exe
Enter test runs : n = 2
Enter the brackets : First<1234>second<1234567>end1
depth= 7
starting index = 18
Enter the brackets : Thrid<123456789>forth<12345>end2
depth= 9
starting index = 6
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…