"Индекс находится вне границ массива" - C#
Формулировка задачи:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Fil ur = new Fil();
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
ur.Creation(textBox1);
}
class Fil
{
public void Creation(TextBox m)
{
File.WriteAllText("doce.txt", m.Text, Encoding.UTF8);
}
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text = File.ReadAllText("doce.txt", Encoding.UTF8);
}
private void button6_Click(object sender, EventArgs e)
{
{
File. WriteAllText("doce.txt", textBox1.Text, Encoding.UTF8);
string input="";
textBox1.Text = input;
listBox1.Items.Clear();
string[] str = input.Split(new Char[] { ' ', ',', '.', ':', '!', '?', ';' }, StringSplitOptions.RemoveEmptyEntries);
int maxlen = 0, index = 0;
for (int i = 0; i < str.Length; i++)
{
if (str[i].Length > maxlen)
{
maxlen = str[i].Length;
index = i;
}
}
listBox1.Items.Add("Самое длинное слово: " +str[index]);
}
}
}
}Решение задачи: «"Индекс находится вне границ массива"»
textual
Листинг программы
string input="";
textBox1.Text = input;
listBox1.Items.Clear();
string[] str = input.Split(new Char[] { ' ', ',', '.', ':', '!', '?', ';' }, StringSplitOptions.RemoveEmptyEntries);
int maxlen = 0, index = 0;
for (int i = 0; i < str.Length; i++)
{
if (str[i].Length > maxlen)
{
maxlen = str[i].Length;
index = i;
}
}
listBox1.Items.Add("Самое длинное слово: " +str[index]);