using System.Collections.Generic;
using System.Linq;
using System.Text;
private IEnumerable<int> lowerCaseRange = Enumerable.Range(97, 122);
private string stringToAlphaString(string source, bool addSpaces)
{
StringBuilder sb = new StringBuilder();
source = source.ToLower();
foreach (char c in source)
{
if (lowerCaseRange.Contains(c))
{
sb.Append(c - 96);
if (addSpaces) sb.Append(' ');
}
}
return sb.ToString();
}
Note that punctuation and numbers will be discarded in this implementation, although it would be easy to add handling them if you are clear about exactly how you want the output formatted.
In this case I made a specific decision to set up an EnumerableRange, rather than implement simpler tests for whether a character was a letter or a digit using:
if(char.IsLetter(c)) ... if(char.IsDigit(c))
The reason I made this choice is so that if I wanted to implement using an additional criterion based on some other range of characters, it would be easy to add.