bri
bri

Reputation: 15

How to show file text in array in c#?

Task: Prepare a program that will form a set (array) of letters used in a text file and display it in alphabetical order on the screen.

I want to make Letters(file); (text in file) to show by one letter. But It shows me 'TextFile1.txt' instead.

Show and Letters methods are given. My code:

static void Main() 
{ 
  string file = "TextFile1.txt"; 
  Show(file); 
  char[] let = Letters(file); 
  foreach (char c in let)
  { 
   Console.WriteLine(c);
  }    
    Console.ReadLine();
}
static void Show(string name)
{
    if (File.Exists(name))
    {
        string text = File.ReadAllText(name, Encoding.GetEncoding(1257));
        Console.WriteLine(text);
    }
    else Console.WriteLine("File {0} is not in disc", name);
}
static char[] Letters(string e)
{
    e = e.ToUpper();
    char[] mas = new char[32];
    int n = 0;
    foreach (char r in e)
        if (Array.IndexOf(mas, r) < 0)
            if (Char.IsLetter(r))
                mas[n++] = r;
    Array.Resize(ref mas, n);
    return mas;
}

}

Upvotes: 1

Views: 65

Answers (1)

Caius Jard
Caius Jard

Reputation: 74635

One of the things that makes this particular assignment easier is that C# treats char and int as equivalent for some contexts and operations; characters have a numerical equivalent - the letter A is 65 in decimal numbers, thanks to historically being located at position 65 in the ASCII table

You can add a character and an integer together; 'A' + 1 is 'B', or 66 if youre looking at them numerically. You can subtract one character from another: 'B' - 'A' is 1

If we are dealing with just a file full of letters, A to Z then that's 26 characters, and they all run sequentially. This sounds like an array, right?

A is 65. Z is 90. This means we actually only need an array that is 91 elements big, to be able to capture any A to Z. 91 is 'Z' + 1. It doesn't really matter whether you think in numbers or chars for this.

Remember, you have to make an array[10] to be able to say array[0] to array[9]; when you make the array its length is 1 more than the largest index number you will use. To store an array you can index by 'Z' you have to make it 'Z'+1 long:

var counts = new int['Z'+1];  

Now how about doing:

counts['A']++; //or could be counts[65], remember that 'A' and 65 are roughly synonymous

Counts has now counted 1 'A'


So where do you get your chars from to go into the counts?

From the file!

foreach(char f in theFileTextAsString){
  counts[f]++;
}

Whatever order the chars come in, they increment the correct slot. If the file has ABA, then at the end of this, counts['A'] is 2, and counts['B'] is 1


Remember that chars can be incremented too, because we can add numbers to them:

for(char c = 'A'; c <= 'Z'; c++){
    Console.Write(counts[c]);
}

That will loop c through all the chars in the English alphabet; c starts out as 'A' and then goes to 'B' and so on, up to/including 'Z'


Have a think about this, and put it all together. Maybe after you've thought about it you can save some memory by having your array run from 'A'-'A' (i.e. 0) to 'Z'-'A' (i.e. 25) instead of having it run from 0 to 'Z'. It would saving a quarter of a kilobyte to get rid of the need to store counts for every char less than 'A' - it wasn't significant enough for me to add confusion to the answer with extraneous math, but once you grok the concept you might want to try adding it in and having a play with chars as numbers etc

Upvotes: 1

Related Questions