-3

I am reading a notepad text and inserting line per line in sLinge. I want to be able to invert the letters (for example: "Hi how are you" --> "uoy era woh iH")

i keep getting this error code: Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.

this is the notepad content :

Anders Hejlsberg, né en décembre 1960, est un programmeur danois. En 1980, il a commencé à écrire des programmes pour le micro-ordinateur Nascom durant sa scolarité à l'Université technique du Danemark d'où il sortit non diplômé; il a en particulier écrit un compilateur Pascal qui a été vendu sous le nom de Blue Label Pascal compiler pour le Nascom-2. Il l'a rapidement réécrit pour CP/M et MS-DOS, et distribué sous le nom de Compass Pascal puis de Poly Pascal. Après avoir été acquis par Borland, il a été distribué sous le nom Turbo Pascal.

Le rachat par Borland de son logiciel a amené Hejlsberg à être un des fondateurs de la société Borland dans laquelle il est resté jusqu'en 1996. Il a continué le développement du Turbo Pascal et est devenu chef de projet lors de l'élaboration du langage Delphi, successeur du Turbo Pascal.

En 1996, il a quitté Borland pour rejoindre Microsoft où il a travaillé sur le langage J++ et les Windows Foundation Classes. Il est le concepteur du Framework .NET.

Il travaille aujourd'hui chez Microsoft comme un chef de projet et architecte logiciel du projet C#, ainsi que du projet TypeScript

Source: https://fr.wikipedia.org/wiki/Anders_Hejlsberg

                        while (!fichier.EndOfStream)
                        {
                            sLigne = fichier.ReadLine();
                            iIndex = sLigne.Length;
                            while (iIndex >= sLigne.Length)
                            {
                                cChar = sLigne[iIndex];
                                sInverse += cChar;
                                iIndex--;
                            }

                            Console.WriteLine(sInverse);
                        }
                        Console.WriteLine(PAUSE);
                        Console.ReadKey();
  • [reverse-string-without-inbuilt-function-and-using-inbuilt-function](https://www.c-sharpcorner.com/blogs/reverse-string-without-inbuilt-function-and-using-inbuilt-function) – Ryan Wilson Oct 28 '22 at 20:34

2 Answers2

0

You can with a help of Linq: to reverse enumeration of char (which is the initial string) all you should do is to perform Reverse(). The you can obtain string from enumeration via string.Concat:

using System.Linq;

...

while (!fichier.EndOfStream)
{
    sLigne = fichier.ReadLine();
    sInverse = string.Concat(sLigne.Reverse());

    Console.WriteLine(sInverse);
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 1
    This accomplishes your original goal, but you need to pay close attention to the other answer (and the one linked above) so you don't make the same mistake again. – madreflection Oct 28 '22 at 20:40
0

An array of length n goes from 0 to n-1, so the accessing sLigne[iIndex] will be out of bound. Do

cChar = sLigne[iIndex - 1]

instead also the while condition idea is also incorrect, the code inside while-loop will only run once and quits. You can also do a normal for-loop.

    for (int i = sLigne.length - 1; i >= 0; i--) {
        sInverse += sLigne[i];
    }
blaccod
  • 71
  • 3