0% found this document useful (0 votes)
24 views12 pages

Assignment 2

Uploaded by

Bipasha Biswas
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)
24 views12 pages

Assignment 2

Uploaded by

Bipasha Biswas
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/ 12

Date:

1. Write a program to enter a name and print the initials.

 Algorithm:

1. Enter the Name of the person


2. Split the string using split function ()
3. Run a loop in the iteration of (i) between (0,(len(a)-1))
4. Converting the initial to upper case
5. Inserting (.) between the initial and adding the last string from index 1
6. Storing the result in b

Source Code:

name=input("Enter Name:")
a=name.split(" ")
b=""
for i in range(len(a)-1):
b=b+a[i][0].upper()+'.'
b=b+a[-1][0].upper()+a[-1][1:]
print(b)

Output:
Set-1:
Enter Name: Supriyo John Roy
S.J.Roy

Set-2:
Enter Name: Aksh Kumar Shah
A.K.Shah

Set-3:
Enter Name:Kunal Pramanik
K.Pramanik
Date:

3. Write a program to enter a string and convert the uppercase


character to lowercase and vice versa.

 Algorithm:

1. Define a string and traverse through it


2. Count the length of the string and store in b
3. If the lower-case character is encountered then convert them in upper case
character using built-in function
4. If the upper-case character is encountered then convert them in upper case
character using built-in function
5. If it's a character other than upper-case or lower-case alphabet take the
character as it is

Source Code:

str=input("Enter a String:")
s=""
b=len(str)
for i in range(0,b):
if(str[i].islower()):
s=s+str[i].upper()
elif(str[i].isupper()):
s=s+str[i].lower()
else:
s=s+str[i]
print("The new string is=",s)
Date:

Output:

Set-1:
Enter a String:Akash Kumar
The new string is= aKASH kUMAR

Set-2:

Enter a String:KUNAl pramanik


The new string is= kunaL PRAMANIK

Set-3:

Enter a String:My Goal is to become Mathematician


The new string is= mY gOAL IS TO BECOME mATHEMATICIAN
Date:

4. Write a program to enter a string and check if it is


palindrome or not.

 Algorithm:
1. Enter a string and store it in a variable
2. Run a loop in the iteration of (i) between (len(a)-1,-1,-1)
3. Find the reverse of the string
4. Now using the if statement check if the reverse order string is same as the
given string i.e. if the string is palindrome or not
5. Print the string is palindrome or not

Source Code :

a=input("Enter a string:")
w=""
for i in range(len(a)-1,-1,-1):
w=w+a[i]
print(w)
if(w==a):
print("It is pallindrome string")
else:
print("It is not a pallindrome string")
Date:

Output:

Set-1:
Enter a name:eye

eye

It is pallindrome string

Set-2:
Enter a name:level

level

It is pallindrome string

Set-3:
Enter a name:Kunal

lanuK

It is not a pallindrome string


Date:

5. Write a program to enter a sentence and print the words


which starts with vowels.

 Algorithm:
1. Enter a sentence and split the string into a list
2. Traverse the list till the length of the string
3. Run a loop in the iteration of (i) between(0,len(string))
4. Convert the given string to uppercase
5. Using the if statement, check if the first letter of each word in the sentence
is vowel i.e.(A,E,I,O,U)
6. Print the list

Source Code:
k=input("Enter a Sentence :")

p=k.split(" ")

for i in range(len(p)):

m=p[i][0]

n=m.upper()

if(n=='A' or n=='E' or n=='I' or n=='O' or n=='U'):

print(p[i][0:])

else:

print("no such word")


Date:

Output:

Set-1:
Enter a Sentence :Higher Algebra Abstract and Linear

Algebra

Abstract

And

Set-2:
Enter a Sentence :Kunal like to wear dennin shirt

no such word

Set-3:
Enter a Sentence :Akash lives in Odisha

Akash

in

Odisha
Date:

2. Write a program to enter a sentence and count the number of


words, alphabets, vowels and digits present in it.

 Algorithm:
1. Enter a string from the user and store it in a variable.
2. Initialize vowels,digits,words,alphabets variables to 0.
3. Use a for loop to traverse through the characters in the string.
4. Use an if statement to check if the character is a vowel or digits or not and
increment the vowel variable or digit variable if it is a vowel or digit.
5. Print no of alphabets , no of words,no of vowels and no of digits in the
string.

Source Code:
chr= input("Enter the string : ")

v= 0

d= 0

a= 0

w= 0

chr = chr.upper()

l= chr.split()

for i in range(0, len(chr)):

if(chr[i] =='A' or chr[i] =='E' or chr[i] =='I' or chr[i]


=='O' or chr[i] =='U'):

v = v + 1
Date:

elif( chr[i] >='0' and chr[i] <='9'):

d = d + 1

for i in range(0,len(chr)):

if(chr[i]>='A' and chr[i]<='Z'):

a = a + 1

print("No of words present in the sentence =",len(l))

print("No of vowels present in the sentence =",v)

print("No of alphabets present in the sentence =",a)

print("No of digits present in the sentence =",d)

Output:

Set-1:
Enter the string : Kunal loves SCC56

No of words present in the sentence = 3

No of vowels present in the sentence = 4

No of alphabets present in the sentence = 13

No of digits present in the sentence = 2

Set-2:
Enter the string : I love to see movies

No of words present in the sentence = 5

No of vowels present in the sentence = 9


Date:

No of alphabets present in the sentence = 16

No of digits present in the sentence = 0

Set-3:
Enter the string : Life is too short to live

No of words present in the sentence = 6

No of vowels present in the sentence = 9

No of alphabets present in the sentence = 20

No of digits present in the sentence = 0


Date:

6. Write a program to enter a sentence and print the


palindrome string in it.

 Algorithm:

1. Enter a sentence

2. Take a empty list

3. Looping through each word in the string by split function

4. Then check if it is palindrome or not i.e. if word is equal to its reverse


then it is palindrome

5. Add palindrome word in list

6. Print the list.

Source Code :

a=input("Enter a sentence: ")

w=[]

for word in a.split():

if word==word[::-1]:

w.append(word)

print("The palindrome strings are : ",w)


Date:

Output :

Set-1:
Enter a sentence: Her eye is so attractive

The palindrome strings are : ['eye']

Set-2:
Enter a sentence: MB mam is so good

The palindrome strings are : ['mam']

Set-3:
Enter a sentence: I refer to her

The palindrome strings are : ['I', 'refer']

You might also like