I have an array of word. What i want accomplish is if there is 'SUCCESS' and i want change to 'XXXXXXX'. As you can see on the loop, i manage to get the length of word it it match 'SUCCESS'. But i can't reverse the loop backward to replace the word that i found. I successfully replace the first latter but not the others. How can i modified the loop to back from last word of 'SUCCESS' and replace it.
public char[,] CrossWord
{
get
{
var table = new char[,]
{
{'S','U','C','C','E','S','S'},
{'E','U','S','S','E','C','U'},
{'U','S','C','C','C','E','C'},
{'S','S','U','C','E','C','C'},
{'S','E','S','S','E','S','S'},
{'U','C','S','E','U','S','S'},
{'C','C','S','S','E','E','S'},
{'S','U','S','S','S','S','E'},
{'U','S','E','S','S','C','S'}
};
return table;
}
}
public char[,] ReplaceWord(string searchWord, string replaceWord)
{
var newReplaceCrossWordTable = (char[,])CrossWord.Clone();
var totalRowNumber = newReplaceCrossWordTable.GetLength(0);
var totalColumnNumber = newReplaceCrossWordTable.GetLength(1);
String word;
word = "SUCCESS";
int len = word.Length;
int R;
int C;
R = 9;
C = 7;
Console.WriteLine("*** Crossword Table *****");
for (var row = 0; row < totalRowNumber; row++)
{
for (var column = 0; column < totalColumnNumber; column++)
{
Console.Write(newReplaceCrossWordTable[row, column]);
if (CrossWord[row, column] != word[0])
{
}
for (int dir = 0; dir < 8; dir++)
{
int k, rd = row + x[dir], cd = column + y[dir];
for (k = 1; k < len; k++)
{
if (rd >= R || rd < 0 || cd >= C || cd < 0)
{
break;
}
if (CrossWord[rd, cd] != word[k])
{
break;
}
rd += x[dir];
cd += y[dir];
}
if (k == len)
{
for (int di = 0; di < 8; di++)
{
int kk, r = row + x[dir], c = column + y[dir];
for (kk = 1; kk < len; kk++)
{
if (rd >= R || rd < 0 || cd >= C || cd < 0)
{
break;
}
r -= x[dir];
c -= y[dir];
newReplaceCrossWordTable[row, column] = 'X';
}
}
}
}
}
Console.WriteLine();
}
Console.WriteLine("************************");
return newReplaceCrossWordTable;
}
}
What I have tried:
As you can see, i successfully get the length of word. I manage to change the 1st latter but not the rest.
*** Crossword Table *****
SUCCESS
EUSSECU
USCCCEC
SSUCECC
SESSESS
UCSEUSS
CCSSEES
SUSSSSE
UXESSCS
************************
*** Crossword Table *****
xxxxxxx
EXSSECU
UXXCCEC
SXUXECC
SXSSXSS
UXSEUXS
CXSSEEX
SXSSSSE
UXESSCS
************************
}
}