Count characters at same position as in English alphabet
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++
// 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 code int main() { string str = "AbgdeF"; cout << findCount(str); return 0; } |
chevron_right
filter_none
Java
// Java program to find number of // characters at same position // as in English alphabets 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.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 |
chevron_right
filter_none
Python3
# 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 alphabets def 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 Code str = 'AbgdeF'print(findCount(str)) # This code is contributed # by SamyuktaSHegde |
chevron_right
filter_none
C#
// C# program to find number of // characters at same position // as in English alphabets using 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 code public static void Main() { string str = "AbgdeF"; Console.Write(findCount(str)); } } // This code is contributed // by Akanksha Rai |
chevron_right
filter_none
PHP
<?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 alphabets function 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 ?> |
chevron_right
filter_none
Output:
5
This article is contributed by Sahil Chhabra. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Remove characters from the first string which are present in the second string
- Print list items containing all characters of a given word
- Find the smallest window in a string containing all characters of another string
- Length of the longest substring without repeating characters
- Print all permutations with repetition of characters
- Interleaving of two given strings with no common characters
- Count words in a given string
- String matching where one string contains wildcard characters
- Find the first non-repeating character from a stream of characters
- Rearrange a string so that all same characters become d distance away
- Given a sorted dictionary of an alien language, find order of characters
- Given a binary string, count number of substrings that start and end with 1.
- Find the longest substring with k unique characters in a given string
- Reverse an array without affecting special characters
- Find all strings formed from characters mapped to digits of a number



