convert letters increase by 1

Noah Aas 320 Reputation points


Mask_range="ABCDEFGHJKLMNPQRSTUVWXYZ"    Not allowed I and O

NewValue:       AA          AB          AG         AH        BA     **Input**

NewValue1:      AB          AC          AH         AJ        BB     **Output**
NewValue2:      AC          AD          AJ         AK        BC     
NewValue3:      AD          AE          AK         AL        BD     
NewValue4:      AE          AF          AL         AM        BF     

Sample, I  have the input = AB
	   I must increase by one
	                   AC -> AD -> AE -> AF
Sample, I  have the input = AG
	   I must increase by one
	                   AG -> AH -> AI (Not allowed) -> AJ -> AK -> AL
   AZ   --> BA -> BB -> BC -> BD

How can I achieve this? The new output?

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,465 questions
{count} votes

Accepted answer
  1. Bruce ( 59,021 Reputation points


        public static string IncValue(string s)
    		if (s == null || s.Length < 2 || s.Length > 2) throw new Exception("invalid format"); 
    		var firstChar = s[0];
    		var lastChar = s[1];
    		if (firstChar < 'A' || firstChar > 'Z') throw new Exception("invalid first char"); 
    		if (lastChar < 'A' || lastChar > 'Z') throw new Exception("invalid last char"); 
    		lastChar += (char) 1;
    		if (lastChar > 'Z') 
    			lastChar = 'A';
    			firstChar += (char) 1;
    			if (firstChar > 'Z') throw new Exception("overflow"); 
    		return firstChar.ToString() + lastChar.ToString();

2 additional answers

Sort by: Most helpful
  1. Michael Taylor 49,701 Reputation points

    This is really no different than how basic math works. This looks like an academic question more than a core app one so we can assume performance isn't critical.


    • The input is limited to 2.
    • The input is already assumed to be in the valid range.
    • The input ZZ isn't going to happen.

    There are many ways to solve this one but a simple algorithm might simply be:

    • If last character is Z then set to A and "add one" to second from last letter.
    • Last character = last character + 1 (simple addition on the Unicode value is sufficient).
    • If new last character is not valid
      • Increment by 1 again, fail the call, whatever?
    0 comments No comments

  2. Hongrui Yu-MSFT 575 Reputation points Microsoft Vendor

    Hi,@Noah Aas. Welcome to Microsoft Q&A. 

    Design idea: Flexibly use the conversion between characters and ASCII

    Specific code implementation

    char[] Mask = { 'I','O'};
    string Fun(string input, char[] masks)
        char[] output = ((char)64+input).ToCharArray(); 
        bool checkResult = false;
        while (!checkResult)
            Add(output, output.Length-1);
            foreach(char mask in masks)
                    checkResult =false;
                    checkResult = true;
        if ((int)output[0] == 64)
            return string.Concat(output.Skip(1).ToArray());
            return string.Concat(output);
    char[] Add(char[] array,int index)
        int result = (int)(array[index]) + 1;
            array[index] = (char)65;
            array[index] = (char)result;
        return array;

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.