Last active
August 29, 2017 06:13
-
-
Save Rudde/81dadbb2f914053a89d9cc57c450cc6b to your computer and use it in GitHub Desktop.
Increment letters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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