Count characters at same position as in English alphabets

1.6

Given a string of lower and uppercase characters, the task is to find that how many characters are at same position as in English alphabets.

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) Travers 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++ 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;

    // Travers 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
        // alphabatic 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;
}

Output:

 5

This article is contributed by Sahil Chhabra(KILLER). 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.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1.6 Average Difficulty : 1.6/5.0
Based on 25 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.