Adding elements to a vector recursively.

I'm attempting to create a recursive function that outputs a vector of strings that contains all possible word combinations (while retaining order of letters) of a given string. Basically, the foundation of an auto-correct typing program, which produces effects similar that of the iPhone.

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
vector<string> allPossibleWords(string str, vector<vector<char> > & adjacentKeys)
{
  vector<string> words;

  cout << str << endl;

  if (str.length() == 0)
  {
    return words;
  }

  char firstLetter = str[0];
  string restOf = str.substr(1, str.length() - 1);
  int position = position_in_vector(firstLetter);

  for (int i = 0; i < adjacentKeys[position].size(); i++) 
  {
    string temp(1, adjacentKeys[position][i]);
    words.push_back(temp);
  }

  //allPossibleWords(restOf, adjacentKeys);
}

int position_in_vector(char letter)
{
  return (letter % 97);
}

For instance, if str is "yp", the output should be a vector containing the values {"yp", "tp", "gp", "hp", "up", "yo", "to", "go", "ho", "uo", "yl", "tl", "gl", "hl", "ul"}. If str is "y", the output should be a vector containing the values {"y", "t", "g", "h", "u"}.

The 26 vectors stored in adjacentKeys contain the letters adjacent to the letter (on a QWERTY keyboard) that is stored in the first position of the vector.

1
2
3
4
5
a   qwsz
b   vghjn
c   xdfgv
d   zserfcx
//and so on 

I am stuck with this function, and can't figure out how to recursively build this vector.
Two things I would do:
Either do the whole thing iteratively, putting the for loop of lines 16-20 inside a larger one.
 
for(int j=0; j<str.size()-1; j++)

Or if you really want it to be recursive, you could remove the for loop entirely and put everything in a string stream to be parsed after.

I'm sure there is some way to recursively build the vector too, I just can't think of it right now.
yea man assignment 5 is some hard shit. i Have no clue what to do about this function to get it to generate the words.

Topic archived. No new replies allowed.