0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        public const int N = 10;
        static void Main(string[] args)
        {
            char[] word = Console.ReadLine().ToCharArray();
            int i = 0, j = 0;
            Console.WriteLine(word);
            while ((word[i] >= 'a' && word[i] <= 'z') || (word[i] >= 'A' && word[i] <= 'Z'))
            {
                j++;
                i++;
            }
            Console.WriteLine(+j);
            Console.ReadLine();

        }
    }
}

Every time I try to debug, the debbuger tells me "IndexOutOfRangeException was unhandled" and I don't know the reason why.

  • 1
    You never check the length of the `word` array. If all the characters are letters, then the loop will continue until `i` is beyond the end of the array and then you will get the exception. – Brian Rogers Oct 25 '17 at 03:17
  • Tip: before asking question make sure to search for error message/exception name (like https://www.bing.com/search?q=c%23+IndexOutOfRangeException), if you still decide to ask question follow [MCVE] guidelines to provide minimal code with all necessary information inline (i.e. in this case `"A".ToCharArray()[1]`") – Alexei Levenkov Oct 25 '17 at 04:26

3 Answers3

3

You are just not checking for the length of the array and continuously checking for elements which cause the IndexOutOfRangeException

Add this condition and it will work

while (i < word.Length && (word[i] >= 'a' && word[i] <= 'z') || (word[i] >= 'A' && word[i] <= 'Z'))
{
   j++;
   i++;
}

You should also know why IndexOutOfRangeException is thrown and what it means refer - https://msdn.microsoft.com/en-us/library/system.indexoutofrangeexception(v=vs.110).aspx

Ipsit Gaur
  • 2,872
  • 1
  • 23
  • 38
2

This is because you are incrementing i and j at each iteration of the while loop, but you never exit the loop.

char[] word = Console.ReadLine().ToCharArray();

What happens when i becomes a value larger than the line read in from the console? You get the IndexOutOfRangeException

Perhaps think of when you would want to stop incrementing i, and break out of the loop.

flyte
  • 1,242
  • 11
  • 18
  • 1
    ..the question smells of homework.. I've answered accordingly and just didn't provide the solution, but rather have the poster think about how to solve. – flyte Oct 25 '17 at 03:22
2

The above answers already provided sufficient information about the problem. I guess I will just add on the outcome.

If you only want to display the alphabets, simply put a checks if the input letters/words are alphabet characters or space then display the letters/words, else return the invalid error message.

Here's the complete tested class for your reference.

using System;
using System.Text.RegularExpressions;

namespace WhileLoop
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            string words = Console.ReadLine();

            //input words
            Console.WriteLine(words);


            //check not alphabet or space, return invalid error message
            Regex rgx = new Regex("[^a-zA-Z ]+");
            if (rgx.IsMatch(words))
            {
                Console.WriteLine("Please input alphabet or space only Ie. A-Z, a-z,");
            }

            Console.ReadLine();

        }
    }
}

Scenario#1 - Input non-alphabet characters enter image description here

Scenario#2 - Input alphabet characters and space (EXPECTED RESULT) enter image description here

Marvin Glenn Lacuna
  • 1,682
  • 1
  • 19
  • 25
  • The OPs code was counting the number of leading alphabetic characters in a string, but this is interesting too. – Rufus L Oct 25 '17 at 04:01