Search code examples
c#incrementalphabet

add operation to string in my own language


I have a word of 5 character like:

"AD5B6" , "1G2H0" , "HASAN" , "ABC5Z" , "1ZZZZ"

I need the new value when i add "1" to my word (like counting system) so the result will be like:

"AD5B7" , "1G2H1" , "HASAO" , "ABC60" , "20000"

I write the code for that but i have the problem in 'Z' character due to i need to increment the previous value. my C# code is:

public string Get(string h)
{
    var lang = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string hash = h.ToUpper();
    char c = hash[hash.Length - 1]; // last char in the word
    string result = "";
    for (int i = 0; i < lang.Length; i++)
    {
        if (c == lang[i] && c != 'Z')
        {
            char cc = lang[i + 1];   // last char + 1
            string s1 = hash.Split(c)[0];
            string s2 = hash.Split(c)[1];
            result = s1 + cc + s2;
            break;
        }
        else
        { // the wrong in this code and i think i should do it in recursive way
            char cc = '0';
            string s1 = hash.Split(c)[0];
            char s11 = s1[s1.Length - 1];
            string s2 = hash.Split(c)[1];
            result = s1 + cc + s2;
            break;
        }
    }
    return result;
}

Solution

  • Seems like you want to do Base36 operations like

    var base36 = new BaseN("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    
    Console.WriteLine( base36.ToBaseNString(base36.FromBaseNString("AD5B7") + 1) );
    Console.WriteLine( base36.ToBaseNString(base36.FromBaseNString("1ZZZZ") + 1) );
    Console.WriteLine( base36.ToBaseNString(base36.FromBaseNString("ZZZZZ") + 1));
    Console.WriteLine( base36.ToBaseNString(base36.FromBaseNString("6ZZZZZZZZZZZZZZZZZZ") + 1) );
    
    Console.WriteLine(base36.ToBaseNString(base36.FromBaseNString("123") * 2));
    

    A generic alogorithm for any charset can be written as

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Numerics;
    
    public class BaseN
    {
        List<char> CHARSET = null;
    
        public BaseN(string charset)
        {
            CHARSET = new List<char>(charset);
        }
    
        public String ToBaseNString(BigInteger input)
        {
            var stack = new Stack<char>();
            while (input != 0)
            {
                stack.Push(CHARSET[(int)(input % CHARSET.Count)]);
                input /= CHARSET.Count;
            }
            return new string(stack.ToArray());
        }
    
        public BigInteger FromBaseNString(string input)
        {
            BigInteger sum = 0;
            int i = 0;
            foreach (char c in input.Reverse())
            {
                sum += CHARSET.IndexOf(c) * BigInteger.Pow(CHARSET.Count, i++);
            }
            return sum;
        }
    }
    

    Although, there are better ways to do bitwise operations in c#, you can also do it as:

    var base2 = new BaseN("01");
    Console.WriteLine( base2.ToBaseNString(base2.FromBaseNString("11") *3 ) );