0% found this document useful (0 votes)
2 views

2.string

The document outlines a Python program that performs various string operations, including finding the longest word, counting character frequency, checking for palindromes, and locating the first occurrence of a substring. It includes functions for each operation and demonstrates their usage through user input. The program also handles string reversal and word counting.

Uploaded by

Yash Dhumal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

2.string

The document outlines a Python program that performs various string operations, including finding the longest word, counting character frequency, checking for palindromes, and locating the first occurrence of a substring. It includes functions for each operation and demonstrates their usage through user input. The program also handles string reversal and word counting.

Uploaded by

Yash Dhumal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

A-5 Write a Python program to compute following operations on String:

a) To display word with the longest length

b) To determines the frequency of occurrence of particular character in the


string

c) To check given string is palindrome or not

d) To display index of first substring

To count the occurrences of each word in string

def In(element, list1):


flag =0
for i in range(len(list1)):
if element == list1[i]:
flag = 1
if flag == 1:
return True
else:
return False

def first_appe(string, substring):


flag=0
for i in range(len(substring)):
if In(substring[i], string):
flag +=1
if flag==len(substring):
for i in range(len(string)):
if substring[0]==string[i]:
return i
return 0

# Function to reverse the given string----------------------------------


>>
def revstr(string):
revstr=""
for i in range(len(string)):
revstr=string[i]+revstr
return revstr

# Function to count the length of given string--------------------------


>>
def str_count(str1, char):
count=0
for i in range(len(str1)):
if char==str1[i]:
count+=1
return count
# Function to find word with longest
length----------------------------------->>
def max_len(splitL):
global new_str2
len1=0
new_str1=""
for i in range (len(splitL)):
if len1 < len(splitL[i]):
len1=len(splitL[i])
new_str2=new_str1+splitL[i]
return len1

# Function to check given string is palindrome or not without reversing


string-->>
def is_palindrome(str):

for i in range(0, int(len(str)/2)):

if str[i] != str[len(str)-i-1]:

return False

return True

str1= input("Enter string to check palindrome:- ")

if is_palindrome(str1):
print ("Entered string is palindrome")
else:
print("Entered string is not palindrome")

# Palindrome checking using reverse string method----------------->>


if revstr(str1)==str1:
print("Entered string is palindrome(using reverse string method)")
else:
print("Entered string is not a palindrome(Using reverse string
method)")
str2= input("Enter sentence to check maximum length of word:")
new=str2.split()
print ("Length: ",max_len(new), "Word: ",new_str2)
str3= input ("Enter string to check perticular character count:")
ch=input ("Enter character: ")
print("Entered character count is: ",str_count(str3, ch))

str4 = input("Enter string to check first appearance of substring: ")


substring= input("Enter substring: ")
print(substring, "is at index: ", first_appe(str4, substring))

You might also like