15,615,155 members
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)
{
}
}
else
{
break;
}
}
return string.concat(chars);
}```
Posted
Updated 4-May-21 3:20am
v2

Solution 1

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();
}
}
```

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)
{
}
else
{
chars[1]++;
}
}
return string.Concat(chars);
}
Maciej Los 4-May-21 9:21am
Good idea!
CPallini 4-May-21 14:27pm
Thank you!

Solution 3

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"
}```

Solution 2

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.

Maciej Los 4-May-21 9:21am
Such of content should be posted as a comment...

Top Experts
Last 24hrsThis month
 OriginalGriff 100 raddevus 75 Richard MacCutchan 40 CHill60 35 Rick York 30
 OriginalGriff 2,996 Richard MacCutchan 1,266 Graeme_Grant 965 Dave Kreskowiak 649 CHill60 360

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