Anagram Solver not solving all words

I'm working on an anagram solver, and started with words 3 characters big. But it can solve some words and not others.

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
29
30
31
32
33
std::string str = "enp";
std::string match = "";
int chk = 0;
std::vector<std::string> words;
words.push_back("the");
words.push_back("ear");
words.push_back("are");
words.push_back("mai");
words.push_back("dad");
words.push_back("mum");
words.push_back("max");
words.push_back("pen");

for (size_t i = 0; i < words.size(); ++i)
{
	for (size_t j = 0; j < str.size(); ++j)
	{
		for (size_t k = 0; k < words[i].size(); ++k)
		{
			if (str[j] == words[i][k])
			{
				chk++;
			}
		}
	}

	if (chk != 3)
		chk = 0;
	else
		match = words[i];
}

std::cout << match;


With the string "enp", it knows it matches "pen". But if I use "mum", it doesn't match it with "mum".

Is there something flawed with my algroithm?
> But if I use "mum", it doesn't match it with "mum".
your algorithm doesn't work with repeated characters.
take a look at the value of `chk' in that case, it doesn't make sense.
In case of "mum", the for the first letter 'm', chk will be incremented twice. You should not be doing that.

As ne555 said, you algorithm will not work for for the words having repeated char. Try printing chk before the the check.
Alright, I managed to get it to work nicely now. I got rid of the chk variable and I use a copy of the string and change the character to null.

Where the code if anyone interested, and if anyone know anything else I can do to make it better.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>

bool IsEmpty(const std::string str)
{
	for (size_t i = 0; i < str.size(); ++i)
	{
		if (str[i] != '\0')
			return false;
	}

	return true;
}

void LoadFile(const std::string s, std::vector<std::string>& v)
{
	std::string str;
	std::ifstream file("wordlist.txt");

	std::cout << "Loading wordlist\n";
	if (file.is_open())
	{
		while (std::getline(file, str))
		{
			if (s.size() == str.size())
			{
				// Converts to lowercase
				std::transform(str.begin(), str.end(), str.begin(), tolower);

				// Puts string into vector
				v.push_back(str);
			}
		}

		file.close();
	}
	else
	{
		std::cout << "Error: Cannot load file!\n";
	}
}

int main()
{
	std::string str = "era";
	std::string tmp = "";
	std::vector<std::string> words;

	LoadFile(str, words);
	std::cout << "Original: " << str << '\n';

	// Check each character with each other
	for (size_t i = 0; i < words.size(); ++i)
	{
		tmp = words[i];

		for (size_t j = 0; j < str.size(); ++j)
		{
			for (size_t k = 0; k < tmp.size(); ++k)
			{
				if (str[j] == tmp[k])
				{
					tmp[k] = '\0';
					break;
				}
			}
		}

		if (IsEmpty(tmp))
		{
			std::cout << words[i] << '\n';
		}
	}

	std::cin.get();
	return 0;
}
sort(a) == sort(b)
Topic archived. No new replies allowed.