I've created a Word scribble solver and it basically computes the permutations of a given word and compares it with the available dictionary. But the problem is I've to copy a char pointer into an array char variable to compare them and I don't know how to do it. Below is my code snippet:
#include<fstream>
#include<string>
#include<ctype>
using namespace std;
void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(char *a, int l, int r)
{
char word[25], word1[25];
fstream flame;
if(l == r)
{
word1 = *a; /* <— Error! */
flame.open("dic.sly", ios::in);
while(!flame.eof())
{
flame << word;
tolower(word[0]);
if(strcmp(word, word1) == 0)
cout << word;
}
flame.close();
}
else
{
for(int i = l; i <= r; i++)
{
swap((a + l), (a + i));
permute(a, l + 1, r);
swap((a + l), (a + i));
}
}
}
void main()
{
char str[] = "lonea";
int n = strlen(str);
permute(str, 0, n - 1);
}
I've quoted where I'm getting the error. Please, correct if there are any mistakes.
#include<string>-- This is not the correct header for the C-style strings. The correct header is<cstring>. The<string>header is forstd::string, which you make no use of in your program whatsoever.using namespace std- it is a bad habit to get into, and can silently change the meaning of your program when you're not expecting it. Get used to using the namespace prefix (stdis intentionally very short), or importing just the names you need into the smallest reasonable scope.