Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

주어진 문자열에서 집합을 사용하여 모음 수를 계산하는 Python 프로그램

<시간/>

이 프로그램에서는 사용자 입력 문자열이 제공됩니다. 이 문자열의 모음 수를 계산해야 합니다. 여기서는 Python에서 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