-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.

4
  • 2
    Plz Indent stackoverflow.com/a/46848194/1465553 Commented Feb 6, 2018 at 3: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: en.wikipedia.org/wiki/Indentation_style#Styles Commented Feb 6, 2018 at 3:38
  • #include<string> -- This is not the correct header for the C-style strings. The correct header is <cstring>. The <string> header is for std::string, which you make no use of in your program whatsoever. Commented Feb 6, 2018 at 3: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 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. Commented Feb 6, 2018 at 14:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This code was written for turbo c++ compiler(academic reason), sorry for incorrect header files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.