Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

โปรแกรม Python นับสระในสตริงด้วย Set (เวอร์ชัน 2)


โปรแกรมนับสระในสตริงผู้ใช้ ใช้ Set ตรวจสอบสมาชิก (ซ้ำกับ ID 1004106508)

ตัวอย่าง

Input: pythonprogram
Output: 3

โค้ดตัวอย่าง

def countvowel(str1):
    c = 0
    v = set("aeiouAEIOU")
    for alpha in str1:
        if alpha in v:
            c += 1
    print("No. of vowels:", c)

str1 = input("Enter the string: ")
countvowel(str1)

ผลลัพธ์

Enter the string: pythonprogram
No. of vowels: 3