I am really new to the idea of recursion, I cannot get this to work properly. It is supposed to return the index of a string if there is a match. For instance: if the user entered "Search me", and I entered this to search for "me". It would return an index of 7. Any help and support would be appreciated.
#include <iostream>
#include <string>
#include <array>
usingnamespace std;
constint ZERO = 0;
constint INCREASE = 1;
constint OUTLIER = -1;
//index_of method
//Purpose:
//Parameters:
//Returns:
int index_of(string stringToSearchIn, string stringToSearchFor, int index)
{
if (stringToSearchIn.length() < stringToSearchFor.length())
{
cout << "Your string cannot be found." << endl;
system("PAUSE");
return OUTLIER;
}
else
{
bool found = true;
for (int i = ZERO; i < stringToSearchFor.length; i++)
{
found = found && stringToSearchIn(i) == stringToSearchFor(i);
}
return index;
}
return index_of(stringToSearchIn.substr(INCREASE), stringToSearchFor, index++);
}
//main method
//Purpose: Main method to call the recursive function index_of
//Parameters: None
//Returns:
int main()
{
//Initializing values
string userString;
string userSearch;
int userIndex = 0;
//Asking for user input for string
cout << "This program will find the occurence of one string inside of another." << endl;
cout << "Enter the string to be searched: " << userString;
//Getting the string
getline(cin, userString);
//Asking for user input for search input
cout << "Now enter the string you want to search for: " << userSearch;
//Getting the string
getline(cin, userSearch);
//Displaying results
cout << "The index of the substring is = " << index_of(userString, userSearch, userIndex);
//Keeping console window open until key press
system("PAUSE");
return ZERO;
}