Python 程式,使用給定字串中的集合統計母音數量
我們使用給定字串中的集合來統計母音數量。假設我們擁有以下輸入 -
jackofalltrades
輸出應如下所示,統計母音數量 -
65
使用給定字串中的集合統計母音數量
我們將使用給定的字串中的集合統計母音數量 -
示例
def vowelFunc(str): c = 0 # Create a set of vowels s="aeiouAEIOU" v = set(s) # Loop to traverse the alphabet in the given string for alpha in str: # If alphabet is present # in set vowel if alpha in v: c = c + 1 print("Count of Vowels = ", c) # Driver code str = input("Enter the string = ") vowelFunc(str)
輸出
Enter the string = howareyou Count of Vowels = 5
不使用函式,使用給定字串中的集合統計母音數量
我們將使用集合不使用函式統計母音數量 -
示例
# string to be checked myStr = "masterofnone" count = 0 print("Our String = ",myStr) # Vowel Set vowels = set("aeiouAEIOU") # Loop through, check and count the vowels for alpha in myStr: if alpha in vowels: count += 1 print("Count of Vowels = ",count)
輸出
Our String = masterofnone Count of Vowels = 5
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP