-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram8.py
More file actions
71 lines (56 loc) · 1.62 KB
/
program8.py
File metadata and controls
71 lines (56 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Menu operations
def count_vowels(s):
count = 0
for ch in s:
if ch.lower() in 'aeiou':
count += 1
return count
def string_length(s):
count = 0
for _ in s:
count += 1
return count
def reverse_string(s):
rev = ""
for ch in s:
rev = ch + rev
return rev
def find_and_replace(s):
find = input("Enter substring to find: ")
replace = input("Enter substring to replace with: ")
return s.replace(find, replace)
def check_palindrome(s):
rev = reverse_string(s)
if s == rev:
return True
else:
return False
string = input("Enter a string: ")
while True:
print("\n--- MENU ---")
print("1. Count number of vowels")
print("2. Count length of string")
print("3. Reverse string")
print("4. Find and replace")
print("5. Check palindrome")
print("6. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
print("Number of vowels:", count_vowels(string))
elif choice == 2:
print("Length of string:", string_length(string))
elif choice == 3:
print("Reversed string:", reverse_string(string))
elif choice == 4:
string = find_and_replace(string)
print("Updated string:", string)
elif choice == 5:
if check_palindrome(string):
print("The string is a palindrome")
else:
print("The string is not a palindrome")
elif choice == 6:
print("Exiting program...")
break
else:
print("Invalid choice! Please try again.")