Заменить цифры словами - C#
Формулировка задачи:
Считать текст из файла и вывести на экран, заменив цифры от 0 до 9 словами «ноль», «один», ..., «девять», начиная каждое предложение с новой строки.
Вроде задание не сильно сложное, но все равно вылетают ошибки. Что я делаю не так?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Lab_8
{
class Program
{
static void Main(string[] args)
{
StreamReader reader = new StreamReader("d:\\file.txt", Encoding.Default);
string[] lines = reader.ReadToEnd().Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
string text = "1 2 3 check9 hello 3";
text = text.Replace("0", "ноль");
text = text.Replace("1", "один");
text = text.Replace("2", "два");
text = text.Replace("3", "три");
text = text.Replace("4", "четыре");
text = text.Replace("5", "пять");
text = text.Replace("6", "шесть");
text = text.Replace("7", "семь");
text = text.Replace("8", "восемь");
text = text.Replace("9", "девять");
text = text.Replace(".", "\n");
text = text.Replace("!", "\n");
text = text.Replace("?", "\n");
text = text.Replace("...", "\n");
Console.WriteLine(text);
reader.Close();
Console.WriteLine();
Console.ReadLine();
}
}
}Решение задачи: «Заменить цифры словами»
textual
Листинг программы
using System;
using System.IO;
using System.Text;
class Program
{
static void Main(string[] args)
{
string[] arr = { "ноль", "один", "два", "три", "четыре", "пять", "шесть", "семь", "восемь", "девять" };
Console.WriteLine("путь к файлу : ");
StreamReader reader = new StreamReader(Console.ReadLine(), Encoding.Default);
string lines = reader.ReadToEnd().Replace('.', '\n');
for (int i = 0; i < 10; i++)lines= lines.Replace(i.ToString(), arr[i]);
Console.WriteLine(lines);
reader.Close();
Console.WriteLine();
Console.ReadKey(true);
}
}