15.8k views
5 votes
Write a function called encrypt(inputStr), which reverses every five characters of the inputstring.Be careful when the length of the input string is not a multiple of 5. This function will reversethe remainder partnevertheless.

User Raj Verma
by
5.3k points

1 Answer

1 vote

Answer:

The following are the program in the Python Programming Language.

#define a function with argument

def encrypt(inputStr):

#print the argument

print(inputStr)

#declare a string type variable

out_Str = ""

#set the for loop to reverse every 5 character of the string

#and store it in the variable

for i in range(0, len(inputStr), 5):

out_Str += inputStr[i:i+5][::-1]

#print the reversed string

print(out_Str)

#call the function with argument

encrypt("")

#print dashes for line

print("------------------------------")

#call the function with argument

encrypt("abc")

#print dashes for line

print("------------------------------")

#call the function with argument

encrypt("abcde")

#print dashes for line

print("------------------------------")

#call the function with argument

encrypt("abcdefg")

#print dashes for line

print("------------------------------")

#call the function with argument

encrypt("abcdefghijkl")

Output:

------------------------------

abc

cba

------------------------------

abcde

edcba

------------------------------

abcdefg

edcbagf

------------------------------

abcdefghijkl

edcbajihgflk

Step-by-step explanation:

The following are the description of the program:

  • Define the function 'encrypt()' and pass an argument 'inputStr' in its parameter and declare the string data type variable 'out_str' .
  • Print the argument of the function before the declaration then set the for loop that reverses the every 5 characters of the string and stores it in the variable 'out_Str' and print that variable.
  • Finally, call the function by following types with printing the dashes for the line.
User Jeff Ogata
by
6.0k points