指定された文字列の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 every alphabet in the given string. Step 4: If the 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プログラム
この記事では、特定の問題ステートメントを解決するための解決策とアプローチについて学習します。 問題の説明 文字列入力が与えられた場合、その文字列がパングラムであるかどうかを確認するPythonプログラムを生成する必要があります。 パングラムは、英語のアルファベットコレクションのすべての文字を含む文/一連の単語です。 では、問題を解決する方法を見てみましょう 入力文字列に存在する各文字が、手動で宣言するアルファベットセットに属しているかどうかをチェックするループを使用します。 上記のアプローチの実装は、-によって与えられます。 例 import string def ispangram