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
|
// Template
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void findX (string s, vector<unsigned>& found, unsigned& idx, unsigned& pos, char& findChar)
{
if (idx != 0){
pos = found[idx-1];
}
found.push_back (s.find(findChar, pos+1));
cout << findChar << " is " << found[idx] << " places into the string." << endl;
if (found[idx]!=string::npos){ // if more matches, then...
idx++;
findX(s, found, idx, pos, findChar); // Recursion
}
}
int main()
{
char findChar = ' ';
unsigned idx = 0;
string str = " ";
unsigned pos = 0;
vector<unsigned> found;
cout << "Please input a string of text: \n";
getline(cin, str);
cout << "Please enter a character to find in this string: ";
cin >> findChar;
findX(str, found, idx, pos, findChar); // Call recusrive function
}
|