Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need a function for getting next string, but the logic is different here. I want to increment input alphabet A to B, B to C until Z. But after Z I need to get string A1. When I get string A1 as input it needs to be incremented to B1, B1 to C1 until Z1. After Z1 I should get A2. When I get A2 it should increment it to B2 and so on till Z9. Can somebody help.

I tried using below code but it gives me wrong output when I give input A1 it gives me output as A2.

What I have tried:

C#
public static string Increment(string input)
{
   List<char> chars = input.ToList();
   for (int i = chars.Count - 1; i >= 0; i++)
   {
      if (chars[i] < '1' || chars[i] > '9')
      {
         throw new ArgumentException("");
      }
      chars[i]++;
      if (chars[i] > 'Z')
      {
         chars[i] = 'A';
         if (i == 0)
         {
            chars.Add('1');
         }
      }
      else
      {
         break;
      }
   }
   return string.concat(chars);
}
Posted
Updated 4-May-21 3:20am
v2

Try (note, such a code is not robust. It is up to you adding the appropriate checks)
C#
public static string Increment(string input)
{
    Char letter = input[0];
    if ( letter < 'Z')
    {
        return ((char)(letter+1)).ToString() + input.Substring(1);
    }
    else
    {
        int number = int.Parse(input.Substring(1))+1;
        return "A"+ number.ToString();
    }
}
 
Share this answer
 
Comments
Member 15181359 3-May-21 11:02am    
Thanks for your code. I am using below one seems like I dont have to chaange much code in it. But will try yours code too.
public static string Increment(string input)
{
List<char> chars = input.ToList();
chars[0]++;
if (chars[0] > 'Z')
{
chars[0] = 'A';
if (input.Length == 1)
{
chars.Add('1');
}
else
{
chars[1]++;
}
}
return string.Concat(chars);
}
Maciej Los 4-May-21 9:21am    
Good idea!
CPallini 4-May-21 14:27pm    
Thank you!
Using Linq:

C#
public static string GetNextCode(string current)
{
	string tmp = current.Length == 1 ? $"{current}0" : current;
	char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToArray();
	char[] digit = "0123456789".ToArray();

	var list = (from d in digit
			from a in alpha
			select $"{a}{d}")
		.ToList();
	int i = list.IndexOf(tmp) +1;
	
	if(i>list.Count-1)
		throw new Exception("Limit of codes has been reached!");
	else
		tmp = list[i];
	
	return tmp.Substring(1,1).Equals("0") ? tmp.Substring(0,1) : tmp;

}


Usage:
C#
void Main()
{
	string currCode = "Z8"; 
	string nextCode = GetNextCode(currCode);
	Console.WriteLine($"{currCode} => {nextCode}"); //prints "A9"
}
 
Share this answer
 
What is the question you're asking? Other than "can somebody help" I can't find a question.

Redesign the logic of the code to give the required output. If you're not certain why it's giving you the output it is, then you haven't walked through the code. Do that and you'll understand what is wrong, and what you need to do to fix it.
 
Share this answer
 
Comments
Maciej Los 4-May-21 9:21am    
Such of content should be posted as a comment...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900