📜  计算字符串中元音的程序(迭代和递归)

📅  最后修改于: 2022-05-13 01:55:17.459000             🧑  作者: Mango

计算字符串中元音的程序(迭代和递归)

给定一个字符串,计算其中元音的总数(a,e,i,o,u)。有两种方法可以计算字符串中元音的总数。
1. 迭代
2.递归
例子:

Input : abc de
Output : 2

Input : geeksforgeeks portal
Output : 7

1. 迭代法
下面是实现:

C++
// C++ program to count vowels in a string
#include
using namespace std;
 
// Function to check the Vowel
bool isVowel(char ch)
{
    ch = toupper(ch);
    return (ch=='A' || ch=='E' || ch=='I' ||
                       ch=='O' || ch=='U');
}
 
// Returns count of vowels in str
int countVowels(string str)
{
    int count = 0;
    for (int i=0; i


Java
// Java program to count vowels in a string
public class GFG {
        
    // Function to check the Vowel
    static boolean isVowel(char ch)
    {
        ch = Character.toUpperCase(ch);
        return (ch=='A' || ch=='E' || ch=='I' ||
                           ch=='O' || ch=='U');
    }
      
    // Returns count of vowels in str
    static int countVowels(String str)
    {
        int count = 0;
        for (int i = 0; i < str.length(); i++)
            if (isVowel(str.charAt(i))) // Check for vowel
                ++count;
        return count;
    }
      
    // Driver code
    public static void main(String args[])
    {
        //string object
        String str = "abc de";
      
        // Total numbers of Vowel
        System.out.println(countVowels(str));
    }
}
// This code is contributed by Sumit Ghosh


Python3
# Python3 program to count vowels
# in a string
 
# Function to check the Vowel
def isVowel(ch):
    return ch.upper() in ['A', 'E', 'I', 'O', 'U']
 
# Returns count of vowels in str
def countVowels(str):
    count = 0
    for i in range(len(str)):
 
        # Check for vowel
        if isVowel(str[i]):
            count += 1
    return count
 
# Driver Code
 
# string object
str = 'abc de'
 
# Total number of Vowels
print(countVowels(str))
 
# This code is contributed
# by SamyuktaSHegde


C#
// C# program to count vowels in a string
using System;
 
class GFG
{
 
    // Function to check the Vowel
    public static bool isVowel(char ch)
    {
        ch = char.ToUpper(ch);
        return (ch == 'A' || ch == 'E' ||
                ch == 'I' || ch == 'O' ||
                              ch == 'U');
    }
 
    // Returns count of vowels in str
    public static int countVowels(string str)
    {
        int count = 0;
        for (int i = 0; i < str.Length; i++)
        {
             
            // Check for vowel
            if (isVowel(str[i]))
            {
                ++count;
            }
        }
        return count;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
         
        //string object
        string str = "abc de";
 
        // Total numbers of Vowel
        Console.WriteLine(countVowels(str));
    }
}
 
// This code is contributed by Shrikant13


PHP


Javascript


C++
// Recursive C++ program to count the total
// number of vowels using recursion
#include
using namespace std;
 
// Function to check the Vowel
bool isVowel(char ch)
{
    ch = toupper(ch);
    return (ch=='A' || ch=='E' || ch=='I' ||
                        ch=='O' || ch=='U');
}
 
// to count total number of vowel from 0 to n
int countVovels(string str, int n)
{
    if (n == 1)
        return isVowel(str[n-1]);
 
    return countVovels(str, n-1) + isVowel(str[n-1]);
}
 
// Main Calling Function
int main()
{
    // string object
    string str = "abc de";
 
    // Total numbers of Vowel
    cout << countVovels(str, str.length()) << endl;
    return 0;
}


Java
// Recursive Java program to count the total
// number of vowels using recursion
public class GFG {
        
    // Function to check the Vowel
    static int isVowel(char ch)
    {
        ch = Character.toUpperCase(ch);
       if(ch=='A' || ch=='E' || ch=='I' ||
            ch=='O' || ch=='U')
           return 1;
       else return 0;
    }
      
    // to count total number of vowel from 0 to n
    static int countVowels(String str, int n)
    {
        if (n == 1)
            return isVowel(str.charAt(n - 1));
      
        return countVowels(str, n-1) + isVowel(str.charAt(n - 1));
    }
      
    // Main Calling Function
    public static void main(String args[])
    {
        //string object
        String str = "abc de";
      
        // Total numbers of Vowel
        System.out.println(countVowels(str,str.length()));
    }
}
// This code is contributed by Sumit Ghosh


Python 3
# Recursive Python 3 program to count the
# total number of vowels using recursion
 
# Function to check the Vowel
def isVowel(ch):
    return ch.upper() in ['A', 'E', 'I', 'O', 'U']
 
# to count total number of
# vowel from 0 to n
def countVovels(str, n):
    if (n == 1):
        return isVowel(str[n - 1]);
 
    return (countVovels(str, n - 1) +
                isVowel(str[n - 1]));
 
# Driver Code
 
# string object
str = "abc de";
 
# Total numbers of Vowel
print(countVovels(str, len(str)))
 
# This code is contributed
# by Akanksha Rai


C#
using System;
 
// Recursive C# program to count the total
// number of vowels using recursion
public class GFG
{
 
    // Function to check the Vowel
    public static int isVowel(char ch)
    {
        ch = char.ToUpper(ch);
       if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
       {
           return 1;
       }
       else
       {
           return 0;
       }
    }
 
    // to count total number of vowel from 0 to n
    public static int countVowels(string str, int n)
    {
        if (n == 1)
        {
            return isVowel(str[n - 1]);
        }
 
        return countVowels(str, n - 1) + isVowel(str[n - 1]);
    }
 
    // Main Calling Function
    public static void Main(string[] args)
    {
        //string object
        string str = "abc de";
 
        // Total numbers of Vowel
        Console.WriteLine(countVowels(str,str.Length));
    }
}
 
// This code is contributed by Shrikant13


PHP


Javascript


输出:

2

辅助空间:O(1)

2.递归方法
下面是实现:

C++

// Recursive C++ program to count the total
// number of vowels using recursion
#include
using namespace std;
 
// Function to check the Vowel
bool isVowel(char ch)
{
    ch = toupper(ch);
    return (ch=='A' || ch=='E' || ch=='I' ||
                        ch=='O' || ch=='U');
}
 
// to count total number of vowel from 0 to n
int countVovels(string str, int n)
{
    if (n == 1)
        return isVowel(str[n-1]);
 
    return countVovels(str, n-1) + isVowel(str[n-1]);
}
 
// Main Calling Function
int main()
{
    // string object
    string str = "abc de";
 
    // Total numbers of Vowel
    cout << countVovels(str, str.length()) << endl;
    return 0;
}

Java

// Recursive Java program to count the total
// number of vowels using recursion
public class GFG {
        
    // Function to check the Vowel
    static int isVowel(char ch)
    {
        ch = Character.toUpperCase(ch);
       if(ch=='A' || ch=='E' || ch=='I' ||
            ch=='O' || ch=='U')
           return 1;
       else return 0;
    }
      
    // to count total number of vowel from 0 to n
    static int countVowels(String str, int n)
    {
        if (n == 1)
            return isVowel(str.charAt(n - 1));
      
        return countVowels(str, n-1) + isVowel(str.charAt(n - 1));
    }
      
    // Main Calling Function
    public static void main(String args[])
    {
        //string object
        String str = "abc de";
      
        // Total numbers of Vowel
        System.out.println(countVowels(str,str.length()));
    }
}
// This code is contributed by Sumit Ghosh

Python3

# Recursive Python 3 program to count the
# total number of vowels using recursion
 
# Function to check the Vowel
def isVowel(ch):
    return ch.upper() in ['A', 'E', 'I', 'O', 'U']
 
# to count total number of
# vowel from 0 to n
def countVovels(str, n):
    if (n == 1):
        return isVowel(str[n - 1]);
 
    return (countVovels(str, n - 1) +
                isVowel(str[n - 1]));
 
# Driver Code
 
# string object
str = "abc de";
 
# Total numbers of Vowel
print(countVovels(str, len(str)))
 
# This code is contributed
# by Akanksha Rai

C#

using System;
 
// Recursive C# program to count the total
// number of vowels using recursion
public class GFG
{
 
    // Function to check the Vowel
    public static int isVowel(char ch)
    {
        ch = char.ToUpper(ch);
       if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
       {
           return 1;
       }
       else
       {
           return 0;
       }
    }
 
    // to count total number of vowel from 0 to n
    public static int countVowels(string str, int n)
    {
        if (n == 1)
        {
            return isVowel(str[n - 1]);
        }
 
        return countVowels(str, n - 1) + isVowel(str[n - 1]);
    }
 
    // Main Calling Function
    public static void Main(string[] args)
    {
        //string object
        string str = "abc de";
 
        // Total numbers of Vowel
        Console.WriteLine(countVowels(str,str.Length));
    }
}
 
// This code is contributed by Shrikant13

PHP


Javascript


输出:

2

递归代码如何工作..