Replace With Alphabet Position

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" = 1, "b" = 2, etc.

Example

alphabetPosition("The sunset sets at twelve o' clock.")
alphabet_position("The sunset sets at twelve o' clock.")
alphabet_position("The sunset sets at twelve o' clock.")
Kata.AlphabetPosition("The sunset sets at twelve o' clock.")
alphabet_position('The sunset sets at twelve o\' clock.');
alphabet_position("The sunset sets at twelve o' clock.");
text:  db  "The sunset sets at twelve o' clock.",0h0

main:
    mov rdi, text
    call alphabet_position
alphabet_position("The sunset sets at twelve o' clock.")
alphabetPosition("The sunset sets at twelve o' clock.")
Kata.alphabetPosition("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 12 22 5 15 3 12 15 3 11" (as a string)

Solutions

🐍 Python

def alphabet_position(text):
    a = ord('a') - 1  #97
    A = ord('A') - 1 #65
    res = ''
    for ch in text:
        if ch.isalpha():
            if ord(ch) > a:
                res+= str(ord(ch) - a)+' '
            else:
                res+= str(ord(ch) - A)+' '

    return res.strip()

👴 C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *alphabet_position(char *text)
{

  //for each char max 2 num code + space
  char* res = (char*)malloc(strlen(text) * sizeof(char) * 3);
  strcpy(res, "");


  if (strlen(text) == 0)
    return res;

  for(int i = 0; i < strlen(text); i++)
  {
    if(isalpha(text[i]))
    {
    //buff - max 4: byte 2 for code 1 for space +1 \n
      char buff[4] = ""; 
      char reducer = (text[i] < 'a' ? 'A' : 'a') - 1;
      printf("%d ", text[i] - reducer );
      sprintf(buff,"%d ", text[i] - reducer );
      strcat(res, buff);
    }
  }

    if(strlen(res) != 0)
      res[strlen(res)-1] = '\0';

    return res;

}

Last updated