Click here to Skip to main content
14,774,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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[,]
                {   //1   2   3   4   5   6    7
                    {'S','U','C','C','E','S','S'},//1
                    {'E','U','S','S','E','C','U'},//2
                    {'U','S','C','C','C','E','C'},//3
                    {'S','S','U','C','E','C','C'},//4
                    {'S','E','S','S','E','S','S'},//5
                    {'U','C','S','E','U','S','S'},//6
                    {'C','C','S','S','E','E','S'},//7
                    {'S','U','S','S','S','S','E'},//8
                    {'U','S','E','S','S','C','S'} //9

                };
                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++)
                        {
                            //CrossWord[row, column] = 'X';
                            // If out of bound break 
                            if (rd >= R || rd < 0 || cd >= C || cd < 0)
                            {
                                break;
                            }

                            // If not matched, break 
                            if (CrossWord[rd, cd] != word[k])
                            {
                                break;
                            }

                            // Moving in particular direction 
                            rd += x[dir];
                            cd += y[dir];
                        }

                        // If all character matched, then value of k 
                        // must be equal to length of word 
                        if (k == len)
                        {

                                //newReplaceCrossWordTable[row, column] = 'X';
                            //Console.Write(newReplaceCrossWordTable[row, column]);

                            for (int di = 0; di < 8; di++)
                            {
                                int kk, r = row + x[dir], c = column + y[dir];


                                for (kk = 1; kk < len; kk++)
                                {
                                    //CrossWord[row, column] = 'X';
                                    // If out of bound break 
                                    if (rd >= R || rd < 0 || cd >= C || cd < 0)
                                    {
                                        break;
                                    }


                                    // Moving in particular direction 
                                    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
************************
//This is what i wanted

*** Crossword Table *****
xxxxxxx
EXSSECU
UXXCCEC
SXUXECC
SXSSXSS
UXSEUXS
CXSSEEX
SXSSSSE
UXESSCS
************************

                            }
}
Posted
Updated 12-Aug-20 7:38am
v9
Comments
OriginalGriff 11-Aug-20 12:16pm
   
You are going to have to explain in a lot more detail what you are trying to do - I for one can't make head or tail out of your description.

So start with what you get as input - the array in "Crossword" I suspect - and expect as output - no idea at all -, then move on to what "reverse the loop" and "replace all the desired characters" actually mean.
Then tell us what you have tried, where you are stuck, and what help you need.

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Member 14912096 11-Aug-20 12:31pm
   
I already updated the question. Hopefully you can understand. Thank you!
Patrice T 11-Aug-20 14:38pm
   
Try to show the wanted result.
Member 14912096 11-Aug-20 23:23pm
   
i already updated with wanted result
Richard MacCutchan 12-Aug-20 14:41pm
   
I am with OriginalGriff on this; I cannot see what criteria you are using for replacing characters with 'X'. In the first line it is OK, you replace the whole word, but in the rest it looks as if you are just replacing random letters.

Quote:
How can i reverse the loop and replace all the desire char after i found the length of the word?

Would be a nice idea to explain what is supposed to do the code with more details.
A sample input/output would be nice too.
Quote:
Already modified the loop many times and still unsuccessfull

Launch with debugger and see with your own eyes your code preforming.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
[Update]
Quote:
I have an array of word. What i want accomplish is if there is 'SUCCESS' and i want change to 'XXXXXXX'.

My translation: I have a matrix of letters, I want to find an hidden word "SUCCESS" (in any direction) in matrix and replace all occurances with "xxxxxxx".
rd += x[dir];
cd += y[dir];

x and y are not defined in your code snipset

Looks like you are messing with variables, you should really go to debugger and watch in detail what your code is doing.
You should investigate the reason why your code do not detect the word on top.
   
v7

My suggestion would be to break your code down into a series of short methods. Assuming that you wish to find SUCCESS in the rows, columns and the left and right diagonals of your table either as SUCCESS or as SSECCUS, try something like this. Set up 2 target arrays one containing the sequence SUCCESS and the other SSECCUS. Go through the rows, columns and diagonals trying to match one or the other of your two target arrays. Here is an example checking the table rows.


private char[] _target = new char[] { 'S', 'U', 'C', 'C', 'E', 'S', 'S' };
private char[] _revTarget = new char[] { 'S', 'S', 'E', 'C', 'C', 'U', 'S' };
private List<Point> _XPoints = new List<Point>();
private int _colCount = 7;
private int _rowCount = 9;
public void CheckRows()
{
    char[] testFrame = new char[_target.Length];
    List<Point> points = new List<Point>();

    for (int r = 0; r < _rowCount; r++)
    {
        int startCol = 0;
        while (startCol + _target.Length <= _colCount)
        {
            points.Clear();
            for (int c = startCol; c < startCol + _target.Length ; c++)
            {
                testFrame[c] = _table[r, c];
                //add the Table reference point (x axis,y axis)
                points.Add(new Point(r, c));
            }

            if (testFrame.SequenceEqual(_target) || testFrame.SequenceEqual(_revTarget))
            {
                //found a match so add the reference points to the main collection
                _XPoints.AddRange(points);
                //when all rows cols and diagonals have been checked
                //use _xPoints to mark an X at each of the points in the table array
            }
            startCol += 1;
        }
    }
}
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900