-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrInvRec.py
More file actions
33 lines (24 loc) · 722 Bytes
/
strInvRec.py
File metadata and controls
33 lines (24 loc) · 722 Bytes
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
from sys import exit
def invert(string):
length = len(string);
if length == 1 or length == 0:
return string;
else:
temp = string[0];
string[0] = string[length - 1];
string[length - 1] = temp;
new_string = string[0:1] + invert(string[1:length - 1]) + string[length-1:];
return new_string;
print "Reverse strings recursively, Enter quit to quit";
while 1:
ip = raw_input("Enter String: ");
if ip == "quit":
exit();
string = ip.split();
string = invert(string);
phrase = '';
phrase += string[0];
for j in string[1:]:
phrase += ' ';
phrase += j;
print "Result: %s" % (phrase);