📌  相关文章
📜  计算给定字符串的元音对

📅  最后修改于: 2021-10-26 06:56:09             🧑  作者: Mango

给定一个由小写英文字母组成的字符串str ,任务是计算相邻元音对的数量。
例子:

做法:从字符串的第一个字符到倒数第二个字符,增量次数开始为每一个字符,其中STR [1]STR [1 + 1]都元音。最后打印计数,这是所需的对数。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that return true
// if character ch is a vowel
bool isVowel(char ch)
{
    switch (ch) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
        return true;
    default:
        return false;
    }
}
 
// Function to return the count of adjacent
// vowel pairs in the given string
int vowelPairs(string s, int n)
{
    int cnt = 0;
    for (int i = 0; i < n - 1; i++) {
 
        // If current character and the
        // character after it are both vowels
        if (isVowel(s[i]) && isVowel(s[i + 1]))
            cnt++;
    }
 
    return cnt;
}
 
// Driver code
int main()
{
    string s = "abaebio";
    int n = s.length();
    cout << vowelPairs(s, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function that return true
    // if character ch is a vowel
    static boolean isVowel(char ch)
    {
        switch (ch) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            return true;
        default:
            return false;
        }
    }
 
    // Function to return the count of adjacent
    // vowel pairs in the given string
    static int vowelPairs(String s, int n)
    {
        int cnt = 0;
        for (int i = 0; i < n - 1; i++) {
 
            // If current character and the
            // character after it are both vowels
            if (isVowel(s.charAt(i)) && isVowel(s.charAt(i + 1)))
                cnt++;
        }
 
        return cnt;
    }
 
    // Driver code
    public static void main(String args[])
    {
        String s = "abaebio";
        int n = s.length();
        System.out.print(vowelPairs(s, n));
    }
}


Python3
# Python3 implementation of the approach
 
# Function that return true
# if character ch is a vowel
def isVowel(ch):
    if ch in ['a', 'e', 'i', 'o', 'u']:
        return True
    else:
        return False
 
# Function to return the count of adjacent
# vowel pairs in the given string
def vowelPairs(s, n):
 
    cnt = 0
    for i in range(n - 1):
 
        # If current character and the
        # character after it are both vowels
        if (isVowel(s[i]) and
            isVowel(s[i + 1])):
            cnt += 1
 
    return cnt
 
# Driver code
s = "abaebio"
n = len(s)
print(vowelPairs(s, n))
 
# This code is contributed
# by mohit kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function that return true
    // if character ch is a vowel
    static bool isVowel(char ch)
    {
        switch (ch)
        {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            return true;
        default:
            return false;
        }
    }
 
    // Function to return the count of adjacent
    // vowel pairs in the given string
    static int vowelPairs(string s, int n)
    {
        int cnt = 0;
        for (int i = 0; i < n - 1; i++)
        {
 
            // If current character and the
            // character after it are both vowels
            if (isVowel(s[i]) && isVowel(s[i + 1]))
                cnt++;
        }
 
        return cnt;
    }
 
    // Driver code
    public static void Main()
    {
        string s = "abaebio";
        int n = s.Length;
         
        Console.WriteLine(vowelPairs(s, n));
    }
}
 
// This code is contributed by Ryuga


PHP


Javascript


输出:
2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程