Assignment 2024
Assignment 2024
1. Write a Python program to add 'ing' at the end of a given string (length
should be at least 3). If the given string already ends with 'ing' then add
'ly' instead. If the string length of the given string is less than 3, leave it
unchanged.
Solution:
def add_string(str1):
length = len(str1)
if length > 2:
if str1[-3:] == 'ing':
str1 += 'ly'
else:
str1 += 'ing'
return str1
str=input("Enter a string(word)")
print("Final word is ",add_string(str))
Output:
Enter a string(word)going
Final word is goingly
Enter a string(word)beautiful
Final word is beautifuling
Output:
Enter a sentence more than 2 wordsTHIS IS A SAMPLE TEXT
THIS SI A ELPMAS TEXT
3. Write a Python program to find the maximum frequency of a character
in a given string.
Solution:
def maxfreq(str1):
ASCII_SIZE = 256
ctr = [0] * ASCII_SIZE
max = -1
ch = ''
for i in str1:
ctr[ord(i)]+=1
for i in str1:
if max < ctr[ord(i)]:
max = ctr[ord(i)]
ch = i
print("character is ",ch,"Frequency is ",max)
str=input("Enter a string")
maxfreq(str)
Output:
Enter a stringabccdfgc
character is c Frequency is 3
4. Write a program to input a line of text and create a new line of text
where each word of input line is reversed.
Solution
str = input("Enter a string: ")
words = str.split()
newStr = ""
for w in words :
rw = ""
for ch in w :
rw = ch + rw
newStr += rw + " "
print(newStr)
Output
Enter a string: Python is Fun
nohtyP si nuF
5. Write a program to input a line of text and print the biggest word (length
wise) from it.
Solution
str = input("Enter a string: ")
words = str.split()
longWord = ''
for w in words :
if len(w) > len(longWord) :
longWord = w