Following Assignment will Perform following operations:
1. Take a string from user and reverse it.
2. Count Number of words in the given string.
3. Count vowels,Consonents and special Charaters in the given string.
4. Reverse each word of the given string.
5. Return Largest word of the string.
IDE Used:- https://www.onlinegdb.com/ π
ββ
Following Assignment will Perform following operations:
1. Take a string from user and reverse it.
2. Count Number of words in the given string.
3. Count vowels,Consonents and special Charaters
in the given string.
4. Reverse each word of the given string.
5. Return Largest word of the string.
IDE Used:- https://www.onlinegdb.com/ π
ββ
#1. This function takes input from user and reverse it.
s = input(βPlease Enter a Sentance: β) # initializing string
print(β1. β,s[::-1])
#2. This function takes input from user and
count the number of the words.
u = len(s.split()) #using split() to count words in string
print (β2. The number of words in string are : β + str(u))
#3.This Function will Count vowels,Consonents and
special Charaters in the given string
b = 0; #b for vowel
h = 0; #h for Consonents
a = 1; #a for special charater
for i in range(0,len(s)):
#Checks whether a character is a vowel
if s[i] in (βaβ,βeβ,βiβ,βoβ,βuβ):
b = b + 1;
elif (s[i] >= βaβ and s[i] <= βzβ):
h = h + 1;
print(β3. Total number of vowel are:β,b,
βTotal Consonents are:β,h,βTotal Special Character are:β,a,β.β)
#4.This Function Reverse each word of the given string.
print(β4. Reverse words of this string are:β)
print(β β,s[::-1])
#5.This Part Return The Largest word of the string.
sentence = s
m = max(sentence.split(), key=len) #Finding longest word
print(β5. Longest word is: β, m) #Displaying longest word
Output:β
PS C:UsersSHEOKAND> & βC:/Program Files/Python39/python.exeβ βe:
/MCA/2 Sem/GJU SEM II/Python/Assignments/A1_Strings.pyβ
Please Enter a Sentance: I am Form Narwana
1. anawraN mroF ma I
2. The number of words in string are : 4
3. Total number of vowel are: 5 Total Consonents are: 6
Total Special Character are: 1 .
4. Reverse words of this string are:
anawraN mroF ma I
5. Longest word is: Narwana
PS C:UsersSHEOKAND>