I have a problem that how can I return a string between ranges(given by user) of char Array. Example:
Entered string is “My name is john".
Start index: 3 Stop index: 6 Function will return “name”
my code is here but i will get address only as a output
#include <iostream>
#include <conio.h>
#include <string>
#include <cstring>
using namespace std;
string *section(char*ary, int index_1, int index_2)
{
string sec=ary;
string *str;
str = &sec;
*str = sec.substr(index_1, index_2);
return str;
}
int main()
{
int starting_index = 0;
int ending_index = 0;
char *ptr;
ptr = new char[200];
int i = 0;
char ch = _getche();
while (ch != 13)
{
ptr[i] = ch;
i++;
ch = _getche();
}
for (int j = 0; j < i; j++)
{
cout << ptr[j];
}
cout << endl;
cout << "Enter start index: " << endl;
cin >> starting_index;
cout << "Enter end index: " << endl;
cin >> ending_index;
cout<<section(ptr, starting_index, ending_index);
delete[] ptr;
system("pause");
}