# Find letter’s position in Alphabet using Bit operation

Given a string of English alphabets. The task is, for every character in the string print its position in the English alphabets.

**Note**: The characters in the string are considered to be case-insensitive. That is, both ‘A’ and ‘a’ are at first position.

**Examples:**

Input:“Geeks”

Output:7 5 5 11 19

‘G’ is the 7^{th}character of the alphabets

‘e’ is the 5^{th}and so on…

Input:“Algorithms”

Output:1 12 7 15 18 9 20 8 13 19

**Approach:**

A letter’s position in Alphabet can easily be found by performing logical **AND** operation with the number **31**.

**Note** that this is only applicable on letters and not special characters.

Every letter has an ASCII value which can be represented in binary form. Performing the bitwise and of this value with the number 31 will give the letter’s position in the alphabets.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `const` `int` `NUM = 31; ` ` ` `// Function to calculate the position ` `// of characters ` `void` `positions(string str, ` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Performing AND operation ` ` ` `// with number 31 ` ` ` `cout << (str[i] & NUM) << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"Geeks"` `; ` ` ` `int` `n = str.length(); ` ` ` ` ` `positions(str, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `public` `class` `GFG { ` ` ` ` ` `public` `static` `final` `int` `NUM = ` `31` `; ` ` ` ` ` `// Function to calculate the position ` ` ` `// of characters ` ` ` `static` `void` `positions(String str, ` `int` `n) ` ` ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` ` ` `// Performing AND operation ` ` ` `// with number 31 ` ` ` `System.out.print((str.charAt(i) & NUM) + ` `" "` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `String str = ` `"Geeks"` `; ` ` ` `int` `n = str.length(); ` ` ` `positions(str, n); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python3 implementation of the approach ` ` ` `NUM ` `=` `31` ` ` `# Function to calculate the position ` `# of characters ` `def` `positions(` `str` `): ` ` ` `for` `i ` `in` `str` `: ` ` ` ` ` `# Performing AND operation ` ` ` `# with number 31 ` ` ` `print` `((` `ord` `(i) & NUM), end ` `=` `" "` `) ` ` ` `# Driver code ` `str` `=` `"Geeks"` `positions(` `str` `) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `public` `static` `int` `NUM = 31; ` ` ` ` ` `// Function to calculate the position ` ` ` `// of characters ` ` ` `static` `void` `positions(` `string` `str, ` `int` `n) ` ` ` `{ ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Performing AND operation ` ` ` `// with number 31 ` ` ` `Console.Write((str[i] & NUM) + ` `" "` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `string` `str = ` `"Geeks"` `; ` ` ` `int` `n = str.Length; ` ` ` `positions(str, n); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to calculate the position ` `// of characters ` `function` `positions(` `$str` `, ` `$n` `) ` `{ ` ` ` `$a` `= 31; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` ` ` `// Performing AND operation ` ` ` `// with number 31$ ` ` ` `print` `((ord(` `$str` `[` `$i` `])&(` `$a` `)).` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `$str` `= ` `"Geeks"` `; ` `$n` `= ` `strlen` `(` `$str` `); ` `positions(` `$str` `, ` `$n` `); ` ` ` `// This code is contributed by 29AjayKumar ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

7 5 5 11 19

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count characters at same position as in English alphabet
- Check if frequency of each character is equal to its position in English Alphabet
- Find the most valued alphabet in the String
- Find the Mid-Alphabet for each index of the given Pair of Strings
- For each lowercase English alphabet find the count of strings having these alphabets
- Find average of two numbers using bit operation
- Find position of the only set bit
- Find the value at kth position in the generated array
- Find position of the given number among the numbers made of 4 and 7
- Find position of left most dis-similar bit for two numbers
- InfyTQ 2019 : Find the position from where the parenthesis is not balanced
- Find the number of occurrences of a character upto preceding position
- Find position of given term in a series formed with only digits 4 and 7 allowed
- Latin alphabet cipher
- Smallest alphabet greater than a given character
- Check if one string can be converted to other using given operation
- Sub-string that contains all lowercase alphabets after performing the given operation
- Count of numbers which can be made power of 2 by given operation
- Check whether the string S1 can be made equal to S2 with the given operation
- Generate number with given operation and check if it is palindrome

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.