I have been working on creating a target solver for the SMH (see http://www.smh.com.au/entertainment/puzzles/target.html).
The program is working well except for the part about:
In making a word, each letter must be used once only.
The function target receives the nine letters in the form 'tetremoei' where the first letter ('t') is the centre letter. The problem I beleive is the if where I call CountCharsStr not the function itself.
I have commented out the irrelevant functions that should not have any effect on fixing the problem.
Code:
int CountCharsStr(char* pstrBuf, char chAscii){
int iCount = 0,iLength = strlen(pstrBuf),iCounter = 0;
for (iCounter=0; iCounter<iLength; iCounter++)
{
if (pstrBuf[iCounter]==chAscii) iCount++;
}
return iCount;
}
int target(char *letterBuf)
{
int printwrd, i, outputwrdcnt = 0;
FILE *text;
char *szLine;
szLine = (char*) malloc(MAX_PATH);
if(szLine==NULL) {
MessageBox(NULL, "Memory couldn't be allocated.", "Target Solver",
MB_ICONEXCLAMATION | MB_OK);
return 1;
}
fflush(stdin);
text = fopen("words.txt","r");
if (!text) {
MessageBox(NULL, "Could not find dictionary.", "Target Solver",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
while(fgets(szLine,MAX_PATH,text) != NULL) {
if (((strlen(szLine) <= 10) && (strlen(szLine) > 4)) && (strchr(szLine,letterBuf[0]))) printwrd = 1;
else continue;
// Filter out Capitals
//Filter out '-', ' ' and ','
for (i=0; i <= strlen(szLine)-2; i++) {
if (!strchr(letterBuf,szLine[i]) || CountCharsStr(szLine,letterBuf[i]) > CountCharsStr(letterBuf,letterBuf[i])) {
printwrd = 0;
break;
}
}
if (printwrd) {
//output szLine
outputwrdcnt++;
}
}
//Output outputwrdcnt
fclose(text);
free(szLine);
return 0;
}