Skip to content

Commit ed30749

Browse files
mayur200cclauss
andauthored
Added swap case program and removed unexpected expression part (#3212)
* Removed an extra '=' which was creating an error while running a program. * Removed the unexpected expression part. * Added program for swap cases in string folder * removed if condition and exchange word with char * added '=' sign which I removed before because of unknowing error from pycharm * added space in test * removed costraint from problem statement * Update cocktail_shaker_sort.py * Update naive_string_search.py * Update swap_case.py * psf/black " not ' * added new line at the end of the file * Fix flake8 issues * added new line at the end of the file * added True and fixed comment * python file end with \n * Update swap_case.py * Update strings/swap_case.py * Update strings/swap_case.py * Apply suggestions from code review * Update strings/swap_case.py * Update swap_case.py * Update swap_case.py Co-authored-by: Christian Clauss <[email protected]>
1 parent fda57d6 commit ed30749

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Diff for: strings/swap_case.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
This algorithm helps you to swap cases.
3+
4+
User will give input and then program will perform swap cases.
5+
6+
In other words, convert all lowercase letters to uppercase letters and vice versa.
7+
For example:
8+
1. Please input sentence: Algorithm.Python@89
9+
aLGORITHM.pYTHON@89
10+
2. Please input sentence: github.com/mayur200
11+
GITHUB.COM/MAYUR200
12+
13+
"""
14+
import re
15+
16+
# This re.compile() function saves the pattern from 'a' to 'z' and 'A' to 'Z'
17+
# into 'regexp' variable
18+
regexp = re.compile("[^a-zA-Z]+")
19+
20+
21+
def swap_case(sentence):
22+
"""
23+
This function will convert all lowercase letters to uppercase letters
24+
and vice versa.
25+
26+
>>> swap_case('Algorithm.Python@89')
27+
'aLGORITHM.pYTHON@89'
28+
"""
29+
new_string = ""
30+
for char in sentence:
31+
if char.isupper():
32+
new_string += char.lower()
33+
if char.islower():
34+
new_string += char.upper()
35+
if regexp.search(char):
36+
new_string += char
37+
38+
return new_string
39+
40+
41+
if __name__ == "__main__":
42+
print(swap_case(input("Please input sentence:")))

0 commit comments

Comments
 (0)