• Дан файл, содержащий произвольный текст. Выяснить, чего в нем больше: русских букв или цифр. Программа на С# уже есть, но нужно заменить на чтение из файла текста с помощью StreamReader и StreamWriter using System; using System.IO; using System.Text; using System.Text.RegularExpressions; class Program3 { static void Main() { Console.WriteLine(\"Введите текст:\"); string _text = Console.ReadLine(); Regex _countLetterRegex = new Regex(@\"[A-zА-яЁё]\"); MatchCollection _countLetterMatch = _countLetterRegex.Matches(_text); int letter = _countLetterMatch.Count; Regex _countNumberRegex = new Regex(@\"[^\\D\\s]\"); MatchCollection _countNumberMatch = _countNumberRegex.Matches(_text); int number = _countNumberMatch.Count; if (letter > number) { Console.WriteLine(\"Количество букв в тексте больше чем цифр\"); Console.WriteLine(\"Количество букв:\" + letter); Console.WriteLine(\"Количество цифр:\" + number); } if (letter < number) { Console.WriteLine(\"Количество цифр в тексте больше чем букв\"); Console.WriteLine(\"Количество букв:\" + letter); Console.WriteLine(\"Количество цифр:\" + number); } Console.WriteLine(); while (true) { if (Console.ReadKey().Key == ConsoleKey.Escape) { Environment.Exit(0); } } } }

Ответы 1

  • String line;

    try

    {

    //Pass the file path and file name to the StreamReader constructor

    StreamReader sr = new StreamReader("C:\\Sample.txt");

    //Read the first line of text

    line = sr.ReadLine();

    //Continue to read until you reach end of file

    while (line != null)

    {

    //write the line to console window

    Console.WriteLine(line);

    //Read the next line

    line = sr.ReadLine();

    }

    //close the file

    sr.Close();

    Console.ReadLine();

    }

    catch(Exception e)

    {

    Console.WriteLine("Exception: " + e.Message);

    }

    finally

    {

    Console.WriteLine("Executing finally block.");

    }

  • Добавить свой ответ

Войти через Google

или

Забыли пароль?

У меня нет аккаунта, я хочу Зарегистрироваться

How much to ban the user?
1 hour 1 day 100 years