Skip to content

Commit b479a8c

Browse files
authored
Merge pull request #21 from AnushkaJagtap22/main
Text Manipulator
2 parents 8082cff + 317d9a1 commit b479a8c

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

basics/text_manipulator.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import re
2+
import base64
3+
4+
def to_upper(text):
5+
return text.upper()
6+
7+
def to_lower(text):
8+
return text.lower()
9+
10+
def capitalize_words(text):
11+
return text.title()
12+
13+
def reverse_text(text):
14+
return text[::-1]
15+
16+
def remove_spaces(text):
17+
return text.replace(" ", "")
18+
19+
def remove_punctuation(text):
20+
return re.sub(r'[^\w\s]', '', text)
21+
22+
def word_count(text):
23+
words = text.split()
24+
return len(words)
25+
26+
def char_count(text):
27+
return len(text)
28+
29+
def vowel_consonant_count(text):
30+
vowels = "aeiouAEIOU"
31+
v = sum(1 for ch in text if ch in vowels)
32+
c = sum(1 for ch in text if ch.isalpha() and ch not in vowels)
33+
return v, c
34+
35+
def find_replace(text, find, replace):
36+
return text.replace(find, replace)
37+
38+
def encrypt_text(text):
39+
return base64.b64encode(text.encode()).decode()
40+
41+
def decrypt_text(text):
42+
try:
43+
return base64.b64decode(text.encode()).decode()
44+
except:
45+
return "Invalid encrypted text!"
46+
47+
# ---------- Main Program ----------
48+
if __name__ == "__main__":
49+
text = input("Enter your text: ")
50+
51+
while True:
52+
print("\n--- TEXT MANIPULATOR MENU ---")
53+
print("1. Convert to UPPERCASE")
54+
print("2. Convert to lowercase")
55+
print("3. Capitalize Each Word")
56+
print("4. Reverse Text")
57+
print("5. Remove Spaces")
58+
print("6. Remove Punctuation")
59+
print("7. Word Count")
60+
print("8. Character Count")
61+
print("9. Vowel & Consonant Count")
62+
print("10. Find & Replace")
63+
print("11. Encrypt Text")
64+
print("12. Decrypt Text")
65+
print("13. Exit")
66+
67+
choice = input("Enter choice: ")
68+
69+
if choice == "1":
70+
print(to_upper(text))
71+
elif choice == "2":
72+
print(to_lower(text))
73+
elif choice == "3":
74+
print(capitalize_words(text))
75+
elif choice == "4":
76+
print(reverse_text(text))
77+
elif choice == "5":
78+
print(remove_spaces(text))
79+
elif choice == "6":
80+
print(remove_punctuation(text))
81+
elif choice == "7":
82+
print("Word count:", word_count(text))
83+
elif choice == "8":
84+
print("Character count:", char_count(text))
85+
elif choice == "9":
86+
v, c = vowel_consonant_count(text)
87+
print("Vowels:", v, "Consonants:", c)
88+
elif choice == "10":
89+
f = input("Word to find: ")
90+
r = input("Replace with: ")
91+
text = find_replace(text, f, r)
92+
print("Updated text:", text)
93+
elif choice == "11":
94+
text = encrypt_text(text)
95+
print("Encrypted:", text)
96+
elif choice == "12":
97+
text = decrypt_text(text)
98+
print("Decrypted:", text)
99+
elif choice == "13":
100+
print("Exiting program...")
101+
break
102+
else:
103+
print("Invalid choice! Try again.")

0 commit comments

Comments
 (0)