Please write code in C++ and include all header files not including std/bitsc:
(Maximum consecutive increasingly ordered substring)
Write a program that prompts the user to enter a string and displays the maximum consecutive increasingly ordered substring. Analyze the time complexity of your program.
Sample Run 1
Enter a string: abcabcdgabxy
The maximum consecutive increasing ordered substring is abcdg
Sample Run 2
Enter a string: abcabcdgabmnsxy
The maximum consecutive increasing ordered substring is abmnsxy
C++ CODE :
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str, res, str2;
cout << "Enter a string : ";
cin >> str; // read the string
for(int i = 0; i < str.length() - 1; i++)
{
res = res + str[i];
for(int j = i; j < str.length() - 1; j++)
{
if(str[j] <= str[j + 1]) // checking consecutive
{
res = res + str[j + 1];
}
else{
break;
}
}
if(res.length() >= str2.length()) // check which is
maximum
{
str2 = res;
}
res = "";
}
cout << str2; // print the result
return 0;
}
OUTPUT :
Get Answers For Free
Most questions answered within 1 hours.