
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find letter's position in Alphabet using Bit operation in C++
In this problem, we are given a string str consisting of english alphabets. Our task is to find the letter's position in the Alphabet using the Bit operation.
Problem Description: Here, we will return the position of each character of the string as it is in english alphabets.
The characters of the string are case-insensitive i.e. “t” and “T” are treated the same.
Let’s take an example to understand the problem,
Input: str = “Tutorialspoint”
Output: 20 21 20 15 18 9 1 12 19 16 15 9 14 20
Solution Approach
A simple solution to find a character's position is by finding its logical AND operation with 31.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; void findLetterPosition(string str, int n) { for (int i = 0; i < n; i++) { cout<<(str[i] & 31) << " "; } } int main() { string str = "TutorialsPoint"; int n = str.length(); cout<<"The letters position in string "<<str<<" is \n"; findLetterPosition(str, n); return 0; }
Output
The letters position in string TutorialsPoint is 20 21 20 15 18 9 1 12 19 16 15 9 14 20
- Related Articles
- Replace a letter with its alphabet position JavaScript
- Swapping letter with succeeding alphabet in JavaScript
- Convert number to alphabet letter JavaScript
- 8086 program to reverse 16 bit number using 8 bit operation
- 8086 program to reverse 8 bit number using 8 bit operation
- Find position of the only set bit in C++
- Write a Golang program to find odd and even numbers using bit operation
- Position of rightmost set bit in C++
- Position of rightmost different bit in C++
- Find position of left most dis-similar bit for two numbers in C++
- Update the bit in the given position or Index of a number using C++
- Position of rightmost common bit in two numbers in C++
- How to shift each letter in the given string N places down in the alphabet in JavaScript?
- Golang Program to find the position of the rightmost set bit
- Find Duplicates of array using bit array in Python

Advertisements