You are on page 1of 7

12. Aim: To write a program to print the largest word in a given string.

Program:
str1=input("Enter a string:")
words=str1.split()
print(words)
max=0
maxword=''
for i in words:
l=len(i)
if max<l:
max=l
maxword=i
print("Largest word:",maxword)

Input/Output:
Enter a string:Flowers are beautiful
['Flowers', 'are', 'beautiful']
Largest word: beautiful

13. Aim: To write a program to input comma separated words and prints unique words in sorted order.

Program:
words=eval(input("Enter comma separated sequence of words:"))
uwords=[]
for i in words:
if i not in uwords:
uwords.append(i)
print(uwords)
uwords.sort()
print("Unique words in sorted form:",uwords)

Input/Output:
Enter comma separated sequence of words:['van','cat','man','van']
['van', 'cat', 'man']
Unique words in sorted form: ['cat', 'man', 'van']

14. Aim: To write a menu driven program to accept a string and perform the following:
-Reverse a string
-Check given character is present or not
-Display string after removing vowels
-Count the occurrences of a character in a string
Program:
str1=input("Enter a string:")
while True:
print('''1.Reverse
2.Character is present
3.Remove vowels
4.Count
5.Exit''')
ch=int(input("Enter your choice:"))
if ch==1:
revstr=''
size=len(str1)
for i in range(-1, -(size+1), -1):
revstr+=str1[i]
print(revstr)
elif ch==2:
chr=input("Enter the character to be searched:")
i=0
size=len(str1)
while i<size:
if str1[i]==chr:
print(chr, "found in string at index:",i)
break
i+=1
else:
print(chr, "found not found in string ")
elif ch==3:
nstr=''
for i in str1:
if i not in "aeiouuAEIOU":
nstr+=i
print("String without vowels:",nstr)
elif ch==4:
chr=input("Enter the character to be searched:")
i=0
c=0
size=len(str1)
while i<size:
if str1[i]==chr:
c+=1
i+=1
print(chr, "found ",c,"times ")
elif ch==5:
break
else:
print("Invalid Input")
Input/Output:
Enter a string:This is a book
1.Reverse
2.Character is present
3.Remove vowels
4.Count
5.Exit
Enter your choice:1
koob a si sihT
1.Reverse
2.Character is present
3.Remove vowels
4.Count
5.Exit
Enter your choice:2
Enter the character to be searched:h
h found in string at index: 1
1.Reverse
2.Character is present
3.Remove vowels
4.Count
5.Exit
Enter your choice:3
String without vowels: Ths s bk
1.Reverse
2.Character is present
3.Remove vowels
4.Count
5.Exit
Enter your choice:4
Enter the character to be searched:i
i found 2 times
1.Reverse
2.Character is present
3.Remove vowels
4.Count
5.Exit
Enter your choice:5

15. Aim: To write a menu driven program to take list from the user and do the following
-Update value of the list
-Insert an element
-Delete an element
-Exit

Program:
l=eval(input("Enter list:"))
print("list is",l)
while True:
print('''1.Update
2.Insert
3.Delete
4.Exit''')
ch=int(input("Enter choice:"))
if ch==1:
ele=int(input("Enter element to be modified:"))
nele=int(input("Enter new element:"))
for i in range(len(l)):
if l[i]==ele:
l[i]=nele
print("Modified List:",l)
elif ch==2:
ele=int(input("Enter element to be inserted:"))
l.append(ele)
print("Modified List:",l)
elif ch==3:
ele=int(input("Enter element to be deleted:"))
for i in range(len(l)):
if l[i]==ele:
del l[i]
break
print("Modified List:",l)
elif ch==4:
break
else:
print("Invalid Input")

Input/Output:
Enter list:[10,20,30]
list is [10, 20, 30]
1.Update
2.Insert
3.Delete
4.Exit
Enter choice:1
Enter element to be modified:20
Enter new element:40
Modified List: [10, 40, 30]
1.Update
2.Insert
3.Delete
4.Exit
Enter choice:2
Enter element to be inserted:50
Modified List: [10, 40, 30, 50]
1.Update
2.Insert
3.Delete
4.Exit
Enter choice:3
Enter element to be deleted:30
Modified List: [10, 40, 50]
1.Update
2.Insert
3.Delete
4.Exit
Enter choice:4

16. Aim: To write a program having nested tuple storing topper details, edit details and display the
details

Program:
toppers=(("Arya","BE",92),("Shyam","BSc",98),("Bala","BTech",97))
ntoppers=()
for i in toppers:
print(i)
ch=input("Do you want to edit details:")
if ch=='y' or ch=='Y':
name=input("Enter name of the student whose details are to be edited:")
nname=input("Enter new name:")
ncourse=input("Enter new course:")
naggr=input("Enter new aggregate:")
i=0
while i < len(toppers):
if toppers[i][0]==name:
ntoppers+=((nname,ncourse,naggr),)
else:
ntoppers+=(toppers[i],)
i+=1
for i in ntoppers:
print(i, end=' ')

Input/Output:
('Arya', 'BE', 92)
('Shyam', 'BSc', 98)
('Bala', 'BTech', 97)
Do you want to edit details:y
Enter name of the student whose details are to be edited:Arya
Enter new name:Ana
Enter new course:BE
Enter new aggregate:76
('Ana', 'BE', '76') ('Shyam', 'BSc', 98) ('Bala', 'BTech', 97)
17. Aim: To write a program to rotate the list elements

Program:
l=eval(input("Enter list:"))
while True:
print('1.Rotate left\n2.Rotate right\n3.Exit')
ch=int(input("Enter choice:"))
if ch==1:
temp=l[0]
for i in range(len(l)-1):
l[i]=l[i+1]
l[len(l)-1]=temp
print(l)
elif ch==2:
temp=l[len(l)-1]
for i in range(len(l)-2,-1,-1):
l[i+1]=l[i]
l[0]=temp
print(l)
elif ch==3:
break
else:
print("Invalid Input")

Input/Output:
Enter list:[1,2,3,4]
1.Rotate left
2.Rotate right
3.Exit
Enter choice:2
[4, 1, 2, 3]
1.Rotate left
2.Rotate right
3.Exit
Enter choice:1
[1, 2, 3, 4]
1.Rotate left
2.Rotate right
3.Exit
Enter choice:3
18. Aim: To write a program to find the frequency of words and store the words as dictionary keys and
frequency as values.

Program:
str1=input("enter string:")
words=str1.split()
print(str1)
print(words)
dict1={}
c=0
for i in words:
if i not in dict1:
dict1[i]=1
else:
dict1[i]+=1
print(dict1)

Input/Output:
enter string:it is a book and a pen
it is a book and a pen
['it', 'is', 'a', 'book', 'and', 'a', 'pen']
{'it': 1, 'is': 1, 'a': 2, 'book': 1, 'and': 1, 'pen': 1}

You might also like