From ed30749943058cfaafeae47bad324200a0e139a0 Mon Sep 17 00:00:00 2001 From: Mayur Pardeshi <45143349+mayur200@users.noreply.github.com> Date: Thu, 15 Oct 2020 03:49:00 +0530 Subject: [PATCH] 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 --- strings/swap_case.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 strings/swap_case.py diff --git a/strings/swap_case.py b/strings/swap_case.py new file mode 100644 index 000000000..71e8aeb3a --- /dev/null +++ b/strings/swap_case.py @@ -0,0 +1,42 @@ +""" +This algorithm helps you to swap cases. + +User will give input and then program will perform swap cases. + +In other words, convert all lowercase letters to uppercase letters and vice versa. +For example: +1. Please input sentence: Algorithm.Python@89 + aLGORITHM.pYTHON@89 +2. Please input sentence: github.com/mayur200 + GITHUB.COM/MAYUR200 + +""" +import re + +# This re.compile() function saves the pattern from 'a' to 'z' and 'A' to 'Z' +# into 'regexp' variable +regexp = re.compile("[^a-zA-Z]+") + + +def swap_case(sentence): + """ + This function will convert all lowercase letters to uppercase letters + and vice versa. + + >>> swap_case('Algorithm.Python@89') + 'aLGORITHM.pYTHON@89' + """ + new_string = "" + for char in sentence: + if char.isupper(): + new_string += char.lower() + if char.islower(): + new_string += char.upper() + if regexp.search(char): + new_string += char + + return new_string + + +if __name__ == "__main__": + print(swap_case(input("Please input sentence:")))