指定された文字列のsetを使用して母音の数をカウントするPythonプログラム
このプログラムでは、ユーザー入力文字列を指定します。この文字列の母音の数を数える必要があります。ここでは、Pythonでsetを使用します。 Setは、反復可能、変更可能で、重複する要素がない、順序付けされていないコレクションデータ型です。
例
Input str1=pythonprogram Output 3
アルゴリズム
Step 1: first we use one counter variable which is used to count the vowels in the string. Step 2: creating a set of vowels. Step 3: then traverse the every alphabet in the given string. Step 4: if alphabet is present in the vowel set then counter incremented by 1. Step 5: After the completion of traversing print counter variable.
サンプルコード
# Program to count vowel in
# a string using set
def countvowel(str1):
c = 0
# Creating a set of vowels
s="aeiouAEIOU"
v = set(s)
# Loop to traverse the alphabet
# in the given string
for alpha in str1:
# If alphabet is present
# in set vowel
if alpha in v:
c = c + 1
print("No. of vowels ::>", c)
# Driver code
str1 = input("Enter the string ::>")
countvowel(str1) 出力
Enter the string ::>pythonprogram No. of vowels ::> 3
-
指定された文字列のセットを使用して母音の数をカウントするPythonプログラム
この記事では、以下に示す問題ステートメントの解決策について学習します。 問題の説明 −文字列が与えられたので、与えられた文字列のセットを使用して母音の数を数える必要があります。 ここでは、文字列全体をトラバースして、各文字が母音であるかどうかを確認し、カウントをインクリメントします。 次に、以下の実装の概念を観察しましょう- 例 def vowel_count(str): count = 0 #string of vowels vowel = "aeiouAEIOU" &nbs
-
Pythonを使用して文字列内の母音の数を数える方法は?
すべての母音を含む文字列オブジェクトを宣言します。 >>> vowels='aeiou' カウント変数を0に初期化するように設定します >>> count=0 入力文字列の各文字が母音文字列に属しているかどうかを確認します。はいの場合、カウントをインクリメントします >>> string='Hello How are you?' >>> for s in string: if s in vowels: c