Skip to content

Instantly share code, notes, and snippets.

@fredyfx
Created June 19, 2018 22:11
Show Gist options
  • Save fredyfx/0eae64f3ad04daccc4b97767b498fe23 to your computer and use it in GitHub Desktop.
Save fredyfx/0eae64f3ad04daccc4b97767b498fe23 to your computer and use it in GitHub Desktop.
Welcome. In this kata you are required to, given a string, replace every letter with its position in the alphabet. If anything in the text isn't a letter, ignore it and don't return it. a being 1, b being 2, etc. As an example: string to input: ""The sunset sets at twelve o' clock.". Should return "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5…
using System;
using System.Linq;
using System.Text;
public static class Kata
{
public static string AlphabetPosition(string text)
{
//First define the groups:
var ValidInput = new char[]{ 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
//Positions in array + 1 = number
StringBuilder sb = new StringBuilder();
foreach (var item in text.ToLower().ToCharArray()){
if(ValidInput.Contains(item)){
sb.Append(Array.IndexOf(ValidInput, item) + 1);
sb.Append(" ");
}
}
return sb.ToString().TrimEnd();;
}
}
using System.Linq;
public static class Kata
{
public static string AlphabetPosition(string text)
{
return string.Join(" ", text.ToLower().Where(char.IsLetter).Select(x => x - 'a'+1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment