String.Copy & String.Substring
Bonjour a tous, je suis débutant en C# et je cherche à faire je vous présente ma fonction :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| public void creationListe()
{
FileStream file = new FileStream("liste.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
StreamReader chaine = new StreamReader(file, Encoding.UTF8);
String mot, canoniser;
List<string> mots;
while (chaine.ReadLine() != null)
{
/* Récupération de chaque mot dans la liste puis canonisation du
mot. Insertion dans le dictionnaire, la clé sera le mot
canonisé. La donnée sera une liste de tous les anagrammes. */
canoniser = "";
mot = chaine.ReadLine();
if (mot.Length != 0)
{
string tmp, tmp2;
for (int pos1 = 0; pos1 < mot.Length; pos1++)
{
tmp = mot.Substring(pos1, 1);
if (canoniser.Length == 0)
{
canoniser = String.Copy(tmp);
}
else
{
for (int pos2 = 1; pos2 <= canoniser.Length; pos2++)
{
tmp2 = canoniser.Substring(pos2, 1);
if (String.Compare(tmp, tmp2) < 0)
{ canoniser.Insert(pos2, tmp); }
}
}
} // Fin du for
if (liste.Count() == 0)
{
mots = new List<string>();
mots.Add(mot);
liste.Add (canoniser,mots);
}
else
{
foreach (KeyValuePair<string, List<string>> element in liste)
{
if (liste.ContainsKey(canoniser))
{
mots = element.Value;
mots.Add(mot);
}
else
{
mots = new List<string>();
mots.Add(mot);
liste.Add(canoniser, mots);
}
} // Fin du foreach
} // Fin du Liste.Count()
} //Fin du if
} // Fin du while
chaine.Close();
file.Close();
} // Fin fonction |
L'exception ArgumentOutOfRangeException à la ligne rouge !! Pour avoir controler la chaine canoniser elle est totalement vide !! J'ai un soucis dans l'affectation de tmp dans canoniser. Un petit soutien dans la création et l'affectation de tmp dans canoniser pourrait m'aider. Mais toutes vos suggestions sur la fonction sont également les bienvenus !!