-2

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.

MarcusS
  • 176
  • 2
  • 10
  • 2
    Plz Indent https://stackoverflow.com/a/46848194/1465553 – Saurav Sahu Feb 06 '18 at 03:37
  • 1
    It would be greatly appreciated if you would fix your indentation to make your code easier to read. Some examples of good indentation can be found here: https://en.wikipedia.org/wiki/Indentation_style#Styles – C_Elegans Feb 06 '18 at 03:38
  • `#include` -- This is not the correct header for the C-style strings. The correct header is ``. The `` header is for `std::string`, which you make no use of in your program whatsoever. – PaulMcKenzie Feb 06 '18 at 03:53
  • You really ought to avoid `using namespace std` - it is a bad habit to get into, and [can silently change the meaning of your program](/q/1452721) when you're not expecting it. Get used to using the namespace prefix (`std` is intentionally very short), or importing *just the names you need* into the *smallest reasonable scope*. – Toby Speight Feb 06 '18 at 14:48

1 Answers1

1

To copy one C-style string to another you have to use strcpy(word1, a);. Only if you use std::string (recommended!) can you assign using =.

You also have a mistake in flame<<word; where you try to write to the input file. To read words you need flame >> word;.

It probably would have been easier to use std::swap instead of implementing your own.

Another bit is tolower(word[0]); which doesn't modify the parameter but returns the result. You ought to save that.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203