Cuente los caracteres en la misma posición que en el alfabeto inglés – Part 1

Dada una string de caracteres en mayúsculas y minúsculas, la tarea es encontrar cuántos caracteres están en la misma posición que en el alfabeto inglés.

Ejemplos: 

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;
}

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

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

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

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
?>

Javascript

<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>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *