-
-
Save PiN73/9536107b22c052af5f7f162f9235e8e6 to your computer and use it in GitHub Desktop.
Get index from address
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 GetIndexFromAddress | |
{ | |
class Program | |
{ | |
static int? NextDigitIndex(string s, int start) | |
{ | |
for (int i = start; i < s.Length; ++i) | |
{ | |
if (char.IsDigit(s[i])) | |
return i; | |
if (!char.IsWhiteSpace(s[i])) | |
return null; | |
} | |
return null; | |
} | |
static int? GetIndex(ref string address) | |
{ | |
int indexEnd = -1; | |
string indexString = ""; | |
for (int digit = 0; digit < 6; ++digit) | |
{ | |
int? i = NextDigitIndex(address, indexEnd + 1); | |
if (i == null) | |
return null; | |
indexString += address[(int) i]; | |
indexEnd = (int) i; | |
} | |
int index = int.Parse(indexString); | |
address = address.Remove(0, indexEnd + 1).TrimStart(',', ' '); | |
return index; | |
} | |
static void Main() | |
{ | |
while (true) | |
{ | |
string address = Console.ReadLine(); | |
int? index = GetIndex(ref address); | |
Console.WriteLine($"index: '{index}'"); | |
Console.WriteLine($"address: '{address}'"); | |
Console.WriteLine(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment