Do your anagram words need to have meaning in dictionary?
Anyway, yes you need to work with the string as a char. You could do a brute-force way using permutation. For example, a given string is '123'. When you look for permutation of any length, you would get '1', '2', '3', '12', '13', '21', '23', '31', '32', '123', '132', '213', 231', '312', '321'.
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
STL has something that can help you: an algorithm that performs permutation. You can apply it to a string, so that you get different words each time, and than comparing to your inner dictionary to see if they form indeed a word. You can save these string in a vector/other form of container.
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
string s="abel's";
do{
cout<<s<<endl;
}while(next_permutation(s.begin(),s.end()));
}
You can also chop the string (saw you mentioned that it could be truncated), than search for it in your dictionary.
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12
Use it with a container that accepts an interator, you can always put all your variables into a vector:
vector<int> array;
and after that you apply that next_permutation function over the array.
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12
When dealing with simple arrays:
int array[7];
you can use next_permutation like this:
#define SIZE 7
int main(){
int array[SIZE]={1,2,3,4,5,6,7};
do{
for (size_t i=0;i<SIZE;i++)
cout<<array[i]<<" ";
cout<<endl;
}while(next_permutation(array, array+SIZE));
return 0;
}
but for strings I would just permute the string's characters, than I'll store them in a container and search in my dictionaries:
vector<string> permute(string a){
vector<string> ret;
//while loop
//do permutations;
ret.push_back(a);//which was permuted;
//
return ret;
}
Than I'll traverse the vector, and look each word in my dictionary.
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12
Question Answered as of 5 Months Ago by
Lucaci Andrew,
T-Dogg3030
and
Taywin