-
Notifications
You must be signed in to change notification settings - Fork 0
/
String Programs.py
107 lines (90 loc) · 2.74 KB
/
String Programs.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'''#******************************* CESAR CIPHER **********************
msg = input("Enter the message: ")
key = int(input("Enter the key value: "))
secret_msg = ""
for char in msg:
if char.isalpha():
code = ord(char)
code += key
if char.isupper():
if code > ord("Z"):
code -= 26
elif code < ord("A"):
code += 26
else:
if code > ord("z"):
code -= 26
elif code < ord("a"):
code += 26
secret_msg += chr(code)
else:
secret_msg += char
print("Encrypted msg: ", secret_msg)
key = -key
original_msg = ""
for char in secret_msg:
if char.isalpha():
code = ord(char)
code += key
if char.isupper():
if code > ord('Z'):
code -= 26
elif code < ord('A'):
code += 26
else:
if code > ord('z'):
code -= 26
elif code < ord('a'):
code += 26
original_msg += chr(code)
else:
original_msg += char
print("Decrypted msg: ", original_msg)
------------------------------------------------------------------------
************************ SYMMETRICAL & PALINDROME ****************************
msg = str(input("Enter the string: "))
half = int(len(msg)/2)
if len(msg)%2 == 0:
first_str = msg[:half]
second_str = msg[half:]
else:
first_str = msg[:half]
second_str = msg[half+1:]
if first_str == second_str:
print(msg,"is Symmetric")
else:
print(msg, "not Symmetric")
if first_str == second_str[::-1]:
print(msg, "is palindrome")
else:
print(msg, "is not palindrome")
********************* REVERSE THE WORDS IN STRING ****************************
string = input("Enter the string: ")
string = string.split(' ')
rev = ' '.join(reversed(string))
print(rev)
------------------------------------------------ FILES ------------------------------
import os
with open("mydata.txt", mode="w", encoding="utf-8") as my_file:
my_file.write("Some random texts\n more and more\nwill see the data")
with open("mydata.txt", encoding="utf-8") \
as my_file:
print(my_file.read())
print(my_file.closed)
---------------------------- '''
msg = str(input("Enter the string: "))
half = int(len(msg)/2)
if len(msg)%2 == 0:
first_str = msg[:half]
second_str = msg[half:]
else:
first_str = msg[:half]
second_str = msg[half+1:]
if first_str == second_str:
print(msg,"is Symmetric")
else:
print(msg, "not Symmetric")
if first_str == second_str[::-1]:
print(msg, "is palindrome")
else:
print(msg, "is not palindrome")