0

I have a text file that looks like this

  • 1234567891
  • a12b13c14d
  • 2122232425
  • 3132333435
  • 4142434445
  • 5152535455
  • 6162636465
  • 7172737475
  • 8182838485
  • 9192939495

in a N x N grid. using c# I need to take the text file and turn it into a 2d array of string so that I can manipulate each character on an independent level. Please help.There is no blank between characters.

String input = File.ReadAllText( @"c:\myfile.txt" );

int i = 0, j = 0;
string[,] result = new string[10, 10];
foreach (var row in input.Split('\n'))
{
    j = 0;
    foreach (var col in row.Trim().Split(' '))
    {
        result[i, j] = int.Parse(col.Trim());
        j++;
    }
    i++;
}

I tried this but there is no spaces between characters. So, I'm thinking about this.

3
  • Console.WriteLine(result[5,3]); is 2 Commented Nov 15, 2022 at 16:16
  • Console.WriteLine(result[1,0]); is a Commented Nov 15, 2022 at 16:17
  • All that has been posted is a program description, but that doesn't tell us what problem you're having. What have you tried, and what troubles did you encounter? Please edit your post to include a valid question that we can answer. Reminder: make sure you know what is on-topic; asking us to write the program for you, opinions, and external links are off-topic. Commented Nov 15, 2022 at 16:18

1 Answer 1

2

I would go with something like this:

var lines = File.ReadAllLines(path);

And now you can access each character. For instance, if you want the 3rd character from line 7, you can get it by lines[6][2].

You will need to add an import, if you have not already:

import System.IO;

If you want to convert it to digits also, you can do a method like this:

// somewhere outside the method you should have read the lines and stored them in a variable:
var lines = File.ReadAllLines(path);

// the method to access a position and convert it to digit
int AccessDigit(string[] lines, int row, int col)
{
    // code that checks if row and col are not outside the bounds should be placed here

    // if inside bounds, we can try to access and convert it
    var isDigit = int.TryParse(lines[row][col], out int digit);

    return isDigit ? digit : -1;
}

And then you would call it like this:

var digit = AccessDigit(lines, 6, 2);

Hope this helps. If my answer still does not help you, please tell me and I'll update my answer.

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

2 Comments

so, I can access the elements in that way, it's really useful thank you. But other question is, how can I print them?
After you have the digit variable (see the last code snippet), you can do Console.WriteLine(digit). If my answer helped you please don't forget to mark it as answer to help others that might encounter the same issue

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.