Count characters at same position as in English alphabet - GeeksforGeeks

29 Apr, 2021
Given a string of lower and uppercase characters, the task is to find that how many characters are at same position as in English alphabet.Examples:  
Input:  ABcED 
Output :  3
First three characters are at same position
as in English alphabets.

Input:  geeksforgeeks 
Output :  1
Only 'f' is at same position as in English
alphabet

Input :  alphabetical 
Output :  3
 
For this we can have simple approach:  
1) Initialize result as 0.
2) Traverse input string and do following for every 
   character str[i]
     a) If 'i' is same as str[i] - 'a' or same as 
        str[i] - 'A', then do result++
3) Return result
 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find number of characters at same// position as in English alphabets#include<bits/stdc++.h>using namespace std; int findCount(string str){    int result = 0;     // Traverse input string    for (int i = 0 ; i < str.size(); i++)         // Check that index of characters of string is        // same as of English alphabets by using ASCII        // values and the fact that all lower case        // alphabetic characters come together in same        // order in ASCII table.  And same is true for        // upper case.        if (i == (str[i] - 'a') || i == (str[i] - 'A'))            result++;      return result;} // Driver codeint main(){    string str = "AbgdeF";    cout << findCount(str);    return 0;}
// Java program to find number of// characters at same position// as in English alphabetsclass GFG{     static int findCount(String str)    {        int result = 0;         // Traverse input string        for (int i = 0; i < str.length(); i++)                 // Check that index of characters        // of string is same as of English        // alphabets by using ASCII values        // and the fact that all lower case        // alphabetic characters come together        // in same order in ASCII table. And        // same is true for upper case.        {            if (i == (str.charAt(i) - 'a')                    || i == (str.charAt(i) - 'A'))            {                result++;            }        }        return result;    }     // Driver code    public static void main(String[] args)    {        String str = "AbgdeF";        System.out.print(findCount(str));    }} // This code is contributed by Rajput-JI
# Python program to find number of# characters at same position as# in English alphabets # Function to count the number of# characters at same position as# in English alphabetsdef findCount(str):    result = 0     # Traverse the input string    for i in range(len(str)):         # Check that index of characters of string is        # same as of English alphabets by using ASCII        # values and the fact that all lower case        # alphabetic characters come together in same        # order in ASCII table. And same is true for        # upper case.        if ((i == ord(str[i]) - ord('a')) or            (i == ord(str[i]) - ord('A'))):            result += 1    return result # Driver Codestr = 'AbgdeF'print(findCount(str)) # This code is contributed# by SamyuktaSHegde
// C# program to find number of// characters at same position// as in English alphabetsusing System; class GFG{static int findCount(string str){    int result = 0;     // Traverse input string    for (int i = 0 ; i < str.Length; i++)         // Check that index of characters        // of string is same as of English        // alphabets by using ASCII values        // and the fact that all lower case        // alphabetic characters come together        // in same order in ASCII table. And        // same is true for upper case.        if (i == (str[i] - 'a') ||            i == (str[i] - 'A'))            result++;     return result;} // Driver codepublic static void Main(){    string str = "AbgdeF";    Console.Write(findCount(str));}} // This code is contributed// by Akanksha Rai
<?php// PHP program to find number of// characters at same position as// in English alphabets // Function to count the number of// characters at same position as// in English alphabetsfunction findCount($str){    $result = 0;     // Traverse the input string    for ($i = 0; $i < strlen($str); $i++)    {         // Check that index of characters of string is        // same as of English alphabets by using ASCII        // values and the fact that all lower case        // alphabetic characters come together in same        // order in ASCII table. And same is true for        // upper case.        if (($i == ord($str[$i]) - ord('a')) or            ($i == ord($str[$i]) - ord('A')))            $result += 1;    }    return $result;} // Driver Code$str = "AbgdeF";print(findCount($str)) // This code has been contributed by 29AjayKumar?>
<script>      // JavaScript program to find number of characters at same      // position as in English alphabets       function findCount(str) {        var result = 0;         // Traverse input string        for (var i = 0; i < str.length; i++)          // Check that index of characters of string is          // same as of English alphabets by using ASCII          // values and the fact that all lower case          // alphabetic characters come together in same          // order in ASCII table. And same is true for          // upper case.          if (            i === str[i].charCodeAt(0) - "a".charCodeAt(0) ||            i === str[i].charCodeAt(0) - "A".charCodeAt(0)          )            result++;         return result;      }       // Driver code      var str = "AbgdeF";      document.write(findCount(str));    </script>
Output:  
 5
 
YouTubeGeeksforGeeks502K subscribersCount characters at same position as in English alphabet | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 1:57•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=MhLeyMLKE3w" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
SamyuktaSHegde
Akanksha_Rai
Rajput-Ji
29AjayKumar
shubham_singh
rdtank
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not
Different methods to reverse a string in C/C++
Convert string to char array in C++
Array of Strings in C++ (5 Different Ways to Create)
Longest Palindromic Substring | Set 1
Caesar Cipher in Cryptography
Check whether two strings are anagram of each other
Length of the longest substring without repeating characters
Reverse words in a given string