Problem Statement:- Write a Python program to compute the following operations on String:
a) To display word with the longest length
b) To determine the frequency of occurrence of a particular character in the string
c) To check whether the given string is palindrome or not
d) To display the index of the first appearance of the substring
e) To count the occurrences of each word in a given string.
s=input("Enter the string: ")
lst=s.split(" ")
def longword():
long=""
for i in range(len(lst)):
if(len(lst[i])>len(long)):
long=lst[i]
print("The longest word in the string is: ", long)
def freqchar():
check=input("Enter the character to be search in the string ")
count=0
for i in range(len(s)):
if(check==s[i]):
count=count+1
print("Character ",check,"occurred in the string ", count, "times")
def palindrome():
s_invert=s[::-1]
if(s_invert.lower()==s.lower()):
print("String is Palindrome")
else:
print("String is not Palindrome")
def freqword():
wordcount={ }
words=list(set(lst))
for i in range(len(words)):
count=0
for j in range(len(lst)):
if(words[i]==lst[j]):
count=count+1
wordcount[words[i]]=count
print("The occurrence of each word in string is as follows",wordcount)
def indexofsubstring():
check=input("Enter the substring to be search in the string ")
for i in range(len(s)):
for j in range(len(check)):
if(check[j]==s[i+j]):
flag=0
else:
flag=1
break
if(flag==0):
print("The index of starting of substring",check, "is ", i)
break
if(flag==1):
print("The substring ", check, "is not found in string")
flag='y'
while(flag=='y'):
print("\n<-----Menu----->")
print("1.Display word with the longest length")
print("2.Find the frequency of occurrence of particular character in the string")
print("3.Check whether string is palindrome or not")
print("4.Display index of first appearance of the substring")
print("5.Count the occurrences of each word in a given string")
n=int(input("Enter the Choice:"))
if(n==1):
longword()
elif(n==2):
freqchar()
elif(n==3):
palindrome()
elif(n==4):
indexofsubstring()
elif(n==5):
freqword()
else:
print("INVALID CHOICE!!!")
flag=input("Do you want to continue?(y/n): ")
if(flag=='n'): print("Thank You!!!")
Comments
Post a Comment