> For the complete documentation index, see [llms.txt](https://anton-veselskyi.gitbook.io/codding-problems-solutions/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://anton-veselskyi.gitbook.io/codding-problems-solutions/codewars/6-kyu/replace-with-alphabet-position.md).

# Replace With Alphabet Position

## [Replace With Alphabet Position](https://www.codewars.com/kata/546f922b54af40e1e90001da)

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

```javascript
alphabetPosition("The sunset sets at twelve o' clock.")
```

```python
alphabet_position("The sunset sets at twelve o' clock.")
```

```ruby
alphabet_position("The sunset sets at twelve o' clock.")
```

```csharp
Kata.AlphabetPosition("The sunset sets at twelve o' clock.")
```

```php
alphabet_position('The sunset sets at twelve o\' clock.');
```

```c
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
```

```rust
alphabet_position("The sunset sets at twelve o' clock.")
```

```scala
alphabetPosition("The sunset sets at twelve o' clock.")
```

```groovy
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

```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

```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;

}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://anton-veselskyi.gitbook.io/codding-problems-solutions/codewars/6-kyu/replace-with-alphabet-position.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
