convert letters increase by 1

Noah Aas 420 Reputation points
2024-06-06T16:23:59.4166667+00:00

Hello,

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
					   
Overflow
   AZ   --> BA -> BB -> BC -> BD

How can I achieve this? The new output?

C#
C#
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,648 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 61,731 Reputation points
    2024-06-07T21:13:41.14+00:00

    simple:

        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 51,346 Reputation points
    2024-06-06T16:55:32.44+00:00

    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.

    Assumptions:

    • 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 1,025 Reputation points Microsoft Vendor
    2024-06-07T03:28:38.13+00:00

    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'};
    
    Console.WriteLine(Fun("AH",Mask));
    
     
    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)
            {
                if(output.Contains(mask))
                {
                    checkResult =false;
                    break;
                }
                else
                {
                    checkResult = true;
                }
            }
        }
    
     
        if ((int)output[0] == 64)
        {
            return string.Concat(output.Skip(1).ToArray());
        }
        else
        {
            return string.Concat(output);
        }
    }
    
    char[] Add(char[] array,int index)
    {
        int result = (int)(array[index]) + 1;
    
        if(result>90)
        {
            array[index] = (char)65;
            Add(array,index-1);
        }
        else
        {
            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.