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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include "Recursion.h"
int index_of(
const string &userString,
const string &searchString,
const size_t index)
{
//decleration of specific datamembers
int len2 = searchString.length();
//compares search string to userString to see if the searchString is present and at what index
//this checks the userString and begins by removing one from each index until it is zero then
//resets and checks the other driection by adding one to index and verify
//if no match is found resturns a -1 to indicate no match found if one is found then returns the location ofr the first match
if ((userString.length() - index) < searchString.length())
return -1;
else if (userString.substr(index, len2) == searchString)
return index;
else
return index_of(userString, searchString, index + 1);
return -1;
}
// Overloading, so you can call index_of with just
//two arguments
int hindex_of(const string &userString, const string &searchString)
{
return index_of(userString, searchString, 0);
}
int main()
{
//Declaration of string variables
string userString = "";
string searchString = "";
//Ask the user for some string they like
cout << "Enter a string that you would like to be analyzed: ";
getline(cin, userString);
cout << endl;
//ask user for a string to find within the string they entered
cout << "Enter the string you are trying to find: ";
getline(cin, searchString);
cout << endl;
hindex_of(userString, searchString);
if (hindex_of(userString, searchString) == -1)
{
cout << "Sorry the string you entered as not found." << endl << endl;
}
else
{
cout << "The index location of " + searchString + " is: " << hindex_of(userString, searchString) << endl << endl;
}
system("PAUSE");
return 0;
};
|