Untitled
unknown
python
4 years ago
2.8 kB
5
Indexable
""" Write a python program to compute following operations on string: 1.To display the word with longest length (DONE) 2.To determine the frequency of of occurrence if a particular character in a string (DONE) 3.To check whether given string is palindrome or not (DONE) 4.To display index of the fist apprearance of substring 5.To count the occurrences of each word in the string """ inp = input("Enter sentence:") #Take input of string. out=inp[::-1] print("The entered string is: ", inp) inp = inp.replace(",", " ") #Replace all the commas with whitespaces. lst = list(inp.strip().split(" "))# Removes all the whitespaces present in the string and constructs a list of words. while True: try: a = input("Enter character: ") # Take input of character that is to be searched in the string. if len(a) == 1 and a.isalpha() == True and a in inp: # Condition to check whether input is character and alphabet. break print("The character is not present in the string\nPlease enter valid character") except Exception as e: print(e) while True: #Conditon to check whether the substring is present in the string. try: b = input("Enter substring: ") if b in inp: break print("The substring is not present in the string\nPlease enter valid substring") except Exception as e: print(e) def max_length(lst): # Function to find the maximum length of word present in string. lst.sort(key=len) print(lst) a=(max(lst,key=len)) print("The longest word present in the string is: ",a) print("The length of longest word in the given string is:", len(a)) def occurrences(inp): # Function to find all the occurrences of the given character in the string. n = 0 for i in inp: if i == a: n = n+1 print("The number of occurrences of the character", a, "is: ", n) def palindrome(inp): #Function to check whether the string is a palindrome or not if inp==out: print("The string is a palindrome") else: print("The string is not a palindrome") def first_app(inp): #Function to print idex of substring in string sub_str = b for i in range(len(inp)): if inp[i] == sub_str[0]: index = i flag = "" for j in range(index, index+len(sub_str)): flag = flag + inp[j] if flag == sub_str: print( 'The index of first appearance of substring is: ', index) break else: pass #def no_of_app(lst): first_app(inp) #no_of_app(lst) max_length(lst) occurrences(inp) palindrome(inp)
Editor is loading...