Skip to content

Instantly share code, notes, and snippets.

@Rudde
Last active August 29, 2017 06:13
Show Gist options
  • Save Rudde/81dadbb2f914053a89d9cc57c450cc6b to your computer and use it in GitHub Desktop.
Save Rudde/81dadbb2f914053a89d9cc57c450cc6b to your computer and use it in GitHub Desktop.
Increment letters
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.Write("Enter letter series: ");
var input = Console.ReadLine();
Console.WriteLine(IncrementLetter(input));
}
}
/**
* Will increment capital letter input,
* eg A -> B, AA -> AB, ZZ -> AAA
**/
private static string IncrementLetter(string input)
{
char[] inputArray = input.ToCharArray();
for (int i = inputArray.Length - 1; i >= 0; i--)
{
if (inputArray[i] == 'Z')
{
if (i == 0)
{
return new string('A', inputArray.Length + 1); // Will print A the length of input string + 1
}
else
{
inputArray[i] = 'A';
}
}
else
{
inputArray[i] = (char) (inputArray[i] + 1);
return new string(inputArray);
}
}
return "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment