Link to home
Start Free TrialLog in
Avatar of ziwez0
ziwez0

asked on

Next Letter ABCD..

Hi,

can someone  please give me an example of how to go to the next letter...
Example if i passed a varaible of A
string strNextLetter = B

or if I passed a W
string strNextLetter = X

I dont really want to do a case statement as there must be a easier way?
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could use an array.

        private const char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }
        private char GetNextLetter(char letter)
        {
            int charIndex = Array.IndexOf<char>(alphabet, letter);
            if(charIndex>-1 && charIndex <= 26) // if we've passed a valid char
                return alphabet[charIndex+1];
            else
                return Char.MinValue;
        }

Open in new window

Without going into a lot of detail, here is a simple way to do it.  Of course, you will need to determine what you want to do when the Starting Character is "Z" or "z".

string strCurrentLetter = "B";
string strNextLetter = "";
string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

if (strCurrentLetter == "Z")
{
// Do something special here
}
else
{
      if (strCurrentLetter == "z")
      {
            // Do something special here
      }
      else
      {
              // Find the position of the Current Letter within the Alphabet
              int intPos = Alphabet.IndexOf(strCurrentLetter);
              // Find the character at the next position
             strNextLetter = Alphabet.Substring(intPos + 1,1);
      }
}
In my example, you will need to include 'using System.Text'  in the code.