 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count of character pairs at same distance as in English alphabets in C++
We are given a string of characters and the task is to calculate the count of character pairs having pairs at the same distance as we have in english alphabets.
Input − string str = ‘Tutorials Point’
Output − Count of character pairs at same distance as in English alphabets are: 5
Explanation − The character pairs with same distance as in english alphabets are (u, t), (u, r), (t, r), (i, o) and (s, n). So in total there are 5 pairs.
Input − string str = ‘Learning is the best habit’
Output − Count of character pairs at same distance as in English alphabets are: 12
Explanation − The character pairs with same distance as in english alphabets are (r, i), (r, h), (n, i), (n, b), (i, g), (n, t), (g, i), (i, b), (s, h), (h, t), (s, t) and (a, b). So in total there are 12 pairs.
Approach used in the below program is as follows
- Input the string of characters and pass the data to the function 
- Take a temporary variable count to store the total of pairs that can be formed 
- Calculate the length of a string using length() function 
- Start loop FOR from i to 0 till the length of a string 
- Inside the loop, start another loop FOR from j to i+1 till the length of a string 
- Inside the loop, set temp as abs(str[i] - str[j]) 
- Check IF temp = abs(i-j) then increment the count by 1 
- Return count 
- Print the result. 
Example
#include <bits/stdc++.h> using namespace std; int pairs_distance(string str){    int count = 0;    int len = str.length();    for (int i = 0; i < len; i++){       for (int j = i + 1; j < len; j++){          int temp = abs(str[i] - str[j]);          if (temp == abs(i - j)){             count++;          }       }    }    return count; } int main(){    string str = "Tutorials Point";    cout<<"Count of character pairs at same distance as in English alphabets are: "<<pairs_distance(str);    return 0; } Output
If we run the above code it will generate the following output −
Count of character pairs at same distance as in English alphabets are: 5
