0% found this document useful (0 votes)
7 views3 pages

Assignment 2024

The document contains a series of Python programming assignments focused on string manipulation. It includes tasks such as appending 'ing' or 'ly' to strings, reversing the middle words of a sentence, finding the maximum frequency of a character, reversing each word in a line of text, and identifying the longest word in a string. Each task is accompanied by a solution and example output.

Uploaded by

it10800223115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Assignment 2024

The document contains a series of Python programming assignments focused on string manipulation. It includes tasks such as appending 'ing' or 'ly' to strings, reversing the middle words of a sentence, finding the maximum frequency of a character, reversing each word in a line of text, and identifying the longest word in a string. Each task is accompanied by a solution and example output.

Uploaded by

it10800223115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment (Based on strings):

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

2. Reverse middle word of a string.


Input : Hi how are you geeks
Output : Hi woh era uoy geeks
Solution:
def printReverse(s):
#Taking all the words in a list
l = [s for s in s.split(' ')]
print(l[0], end = ' ')
for i in range(1, len(l)-1):
print(l[i][::-1], end = ' ')
print(l[len(l)-1])

s=input("Enter a sentence more than 2 words")


printReverse(s)

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

print("Longest Word =", longWord)


Output
Enter a string: TATA FOOTBALL ACADEMY WILL PLAY AGAINST MOHAN BAGAN
Longest Word = FOOTBALL

You might also like