You are on page 1of 3

EXPERIMENT:- 11

Program no.1: WAP to reverse the string using:


a) for loop
def reverse_string(str):
str1 = ""
for i in str:
str1 = i + str1
return str1
str = "AMAN"
print("The original string is: ",str)
print("The reverse string is",reverse_string(str))

Output:-

b) while loop
str = "JavaTpoint"
print ("The original string is : ",str)
reverse_String = ""
count = len(str)
while count > 0:
reverse_String += str[ count - 1 ]
count = count - 1
print ("The reversed string using a while loop is :
",reverse_String)

Output:-
c) Slice Operator
def reverse(str):
str = str[::-1]
return str
s = "JavaTpoint"
print ("The original string is : ",s)
print ("The reversed string using extended slice operator
is : ",reverse(s))

Output:-

Program no. 2: WAP to print Character at odd position and Even position.

number_of_strings = int(input("Enter no of strings: "))


for line in range(number_of_strings):
string = input("Enter string: ")
even_string = ""
odd_string = ""
for i in range(len(string)):
if i%2==0:
even_string = even_string + string[i]
else:
odd_string = odd_string + string[i]
print(even_string,odd_string)

Output:-

Program no. 3: WAP to merge character of string into a single string by taking
character alternatively.

You might also like