Click here to Skip to main content
14,691,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir,

how to solve Index was outside the bounds of the array in c# language.

this is my code

string ar;
            char letter;
            ar = Convert.ToString(Console.ReadLine());
            char[] array = ar.ToCharArray();
            for (int i = 0; i <=ar.Length; i++)
            {
                    if (array[i] == ' ')
                    {
                        Console.WriteLine("Word break:");

                    }
                    else
                    {
                        letter = array[i];
                        Console.Write(letter);
                    }
            }
            Console.ReadKey();

        }

error in my code is Index was outside the bounds of the array.
how to solve it,,,,,,,
Posted

1 solution

Use only <; not <=!
for (int i = 0; i <ar.Length; i++)

C# is using zero-based indexing: 0..n-1
   
v2
Comments
Arsalaan Ahmed 17-Mar-13 3:41am
   
Thanks Zoltán Zörgő problem solved,,,,,,
Zoltán Zörgő 17-Mar-13 6:13am
   
Than feel free to accept my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900