6

I'm trying to fill an array with characters from a string inputted via console. I've tried the code bellow but it doesnt seem to work. I get Index out Of Range exception in the for loop part, and i didn't understand why it occured. Is the for loop range incorrect? Any insight would be greatly appreciated

            Console.WriteLine("Enter a string: ");
            var name = Console.ReadLine();

            var intoarray = new char[name.Length];
            for (var i = 0; i <= intoarray.Length; i++)
            {
                intoarray[i] = name[i];
            }
            foreach (var n in intoarray)
                Console.WriteLine(intoarray[n]);
2
  • Convert name to ToCharArray() directly. Commented Oct 5, 2018 at 3:06
  • 1
    also, as you start from 0, you need to stop at intoarray.Length - 1, so your for-loop should by like for (var i = 0; i < intoarray.Length; i++) Commented Oct 5, 2018 at 3:33

6 Answers 6

9

using ToCharArray() strings can be converted into character arrays.

Console.WriteLine("Enter a string: ");
var name = Console.ReadLine();

var intoarray= name.ToCharArray();

foreach (var n in intoarray)
    Console.WriteLine(n);

if you are using foreach, you should wait for the index to behave as if you were taking the value.

Console.WriteLine(n);
Sign up to request clarification or add additional context in comments.

Comments

3

Since arrays start at 0 and you are counting inclusive of length, the last iteration will exceed the bounds. Just update the loop conditional to be less than length rather than less than or equal to..

Comments

2

I like snn bm's answer, but to answer you question directly, you're exceeding the length of the input by one. It should be:

        for (var i = 0; i <= intoarray.Length - 1; i++)

(Since strings are zero-indexed, the last character in the underlying array is always going to be in the position of arrayLength - 1.)

Comments

1
  • 1: the iteration should be for (var i = 0; i < intoarray.Length; i++)

  • 2: the code

    foreach (var n in intoarray) Console.WriteLine(intoarray[n]);

    also throws an exception, for "n" is a character in the array while it's used as array index.

  • 3: In addition there's a much easier way to convert string to char array

    var intoarray = name.ToCharArray();

    Here's the result

Comments

0

Here is your mistake. There are so many options to represent i < intoarray.Length.

for (var i = 0; i < intoarray.Length; i++) // original was i <= intoarray.Length in your code
{
    intoarray[i] = name[i];
}

Comments

-1

With linq:

  // Select all chars
  IEnumerable<char> intoarray =  
    from ch in name  
      select ch;  // can use var instead of IEnumerable<char>

    // Execute the query  
    foreach (char temp in intoarray)  
        Console.WriteLine(temp);  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.