Tom
Tom

Reputation: 5

How to read from text file different lines and what and how many vowels are in the text presented

private void Button3_Click(object sender, EventArgs e)
        {
            string start = Directory.GetCurrentDirectory() + @"\file.txt";

            using (var streamReader = new StreamReader(start))
            {
                string[] lines = File.ReadAllLines(start);

                int len = lines.Length;

                for (int i = 0; i < len; i++)
                {

                    if (ReadText[i] == 'a' || ReadText[i] == 'e' || ReadText[i] == 'i' || ReadText[i] == 'o' || ReadText[i] == 'u' || ReadText[i] == 'A' || ReadText[i] == 'E' || ReadText[i] == 'I' || ReadText[i] == 'O' || ReadText[i] == 'U')
                    {
                        vowel++;
                    }
                }
                richTextBox3.AppendText(Convert.ToString(vowel) + " ");
            }
        }

How to read from text file different lines and what and how many vowels are in the text presented. Firstly, I'm trying to get access to 5-6-7 lines from this text file: https://gyazo.com/7618175d9c948eb73d0eae5ad322e9c8
And then i want to scan 5-6-7 lines and see what and hwo many vowels in it. And I want to print it in TextBox like this: just for example: a is: 5 \n e is: 7 \n ....

Upvotes: 0

Views: 128

Answers (3)

user10216583
user10216583

Reputation:

How about:

var filePath = Directory.GetCurrentDirectory() + @"\file.txt";
var count = 0;
var vowels = new[] { 'a', 'e', 'i',  'o', 'u' };

foreach(var line in File.ReadLines(filePath).Skip(4).Take(3))
    count += line.Count(c => vowels.Contains(char.ToLowerInvariant(c)));

Console.WriteLine(count);

This will get the required lines only from the text file (5, 6, and 7) and get the total count of the vowels from them.

If you want to get the count of each vowel, then you could do instead:

var dict = new Dictionary<char, int>
{
    { 'a', 0 },
    { 'e', 0 },
    { 'i', 0 },
    { 'o', 0 },
    { 'u', 0 },
};

foreach(var line in File.ReadLines(filePath).Skip(4).Take(3))
    foreach(var kvp in dict.ToList())
        dict[kvp.Key] += line.Count(c => kvp.Key.Equals(char.ToLowerInvariant(c)));

foreach (var kvp in dict)
    Console.WriteLine($"{kvp.Key} : {kvp.Value}");

Upvotes: 1

NetMage
NetMage

Reputation: 26927

First, create a data structure to test for vowels:

var vowels = "aeiou".ToHashSet();

Then a data structure to remember which lines are of interest:

var interestingLineNumbers = new[] { 5, 6, 7 }.ToHashSet();
var maxInterestingLineNumber = interestingLineNumbers.Max();

Now you can pull the interesting lines from the file by reading up to the last interesting line, and keeping the lines of interest:

var linesOfInterest = File.ReadLines(filename).Take(maxInterestingLineNumber).Where((l,i) => interestingLineNumbers.Contains(i+1));

Now for each line of interest, pull out the vowels and then count the total vowels:

var vowelCounts = linesOfInterest.SelectMany(l => l.ToLower().Where(ch => vowels.Contains(ch)))
                                 .GroupBy(vch => vch)
                                 .OrderBy(vg => vg.Key) // optional ordering
                                 .Select(vg => $"{vg.Key} is: {vg.Count()}");

Combine the vowel lines to create the TextBox text value:

var text = String.Join('\n', vowelCounts);

If it isn't important, you could leave out the ordering by removing the OrderBy line. If you need to remember whether the vowel was upper or lowercase, you could extend vowels to include the uppercase vowels and remove the ToLower. Unicode vowel identification is problematic, so I just went with listing the ones you care about.

Upvotes: 1

Jawad
Jawad

Reputation: 11364

You can read all lines and then pick line 5, 6 and 7. Then check vowels in all.

string[] ReadLines = File.ReadAllLines(start);
string line = string.Empty;

if (ReadLines.Count() > 7)
{
    line = $"{ReadLines[4]}{ReadLines[5]}{ReadLines[6]}".ToLower();
}
else
{
    line = string.Join("", ReadLines);
}

int len = line.Length;
for (int i = 0; i < len; i++)
{
    if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u')
    {
        vowel++;
    }
}

richTextBox3.AppendText(vowel.ToString() + " ");


Upvotes: 0

Related Questions