0% found this document useful (0 votes)
5 views2 pages

Lab Programs String

Uploaded by

nazeema.mca06
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)
5 views2 pages

Lab Programs String

Uploaded by

nazeema.mca06
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/ 2

List of programs suggested by CBSE for String Manipulation.

1. Write a program that reads a string and checks whether it is a palindrome string or not.(Th, Pr)
2. Write a program to convert the case of characters. (Title, Alternate) (Th, Pr)
3. Write a program to count the number of vowels, consonants, uppercase, lowercase. (Th, Pr)

ANSWERS
1. Write a program that reads a string and checks whether it is a palindrome string or not.(Th, Pr)
str=input("Enter a sentence:")
n=len(str)
mid=n//2 OUTPUT:
rev=-1 Enter a sentence: madam
for i in range(mid): The given string madam is a palindrome
if str[i] == str[rev]:
i=i+1
rev=rev-1
print("The given string",str, "is a palindrome")
break
else:
print("The given string",str, "is not a palindrome")
break

2. Write a program to convert the case of characters. (Th, Pr)


(a)
str=input("Enter the string:")
new_str='' #empty string
length=len(str)
for i in range(0, len(str)):
if str[i].isupper():
new_str+=str[i].lower()
else:
new_str+=str[i].upper()
print("Original string is:", str)
print("Resulted string is:", new_str)

OUTPUT:
Enter the string: GooD EveNinG!
Original string is: GooD EveNinG!
New string after conversion to title case is: gOOd eVEnINg!

b) #Converting the case of alternate character of the string. Or converting


the case of the even position character of the string.
str=input("Enter the string:")
new_str='' #empty string
length=len(str)
for i in range(0, len(str)):
if i%2==0: #checking the even positions
if str[i].isupper():
new_str+=str[i].lower()
else:
new_str+=str[i].upper()
else:
new_str+=str[i]
print("Resulted string is:", new_str)

OUTPUT:
Enter the string: hello world
Resulted string is: HeLlO WoRlD
1
c) #Converting the case of alternate character of the string. Or converting
the case of the odd position character of the string.
str=input("Enter the string:")
new_str='' #empty string OUTPUT:
length=len(str) Enter the string: hello world
for i in range(0, len(str)): Resulted string is: hElLo wOrLd
if i%2!=0: #checking the odd positions
if str[i].isupper():
new_str+=str[i].lower()
else:
new_str+=str[i].upper()
else:
new_str+=str[i]
print("Resulted string is:", new_str)

3. Write a program to count number of vowels, consonants, uppercase, lowercase. (Th, Pr)
str=input("Enter the string:")
n_cons= n_vows=n_ucase=n_lcase=0
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
space_char=[' ', '\n', '\t', '\r']
for ch in str:
if ch in vowels:
n_vows+=1
else:
if ch not in space_char:
n_cons+=1
if ch.isupper():
n_ucase+=1
if ch.islower():
n_lcase+=1

print("Number of upper-case letters in the given string is:", n_ucase)


print("Number of lower-case letters in the given string is:", n_lcase)
print("Number of vowels in the given string is:", n_vows)
print("Number of consonants in the given string is:", n_cons)

OUTPUT:
Enter the string: hello world
Number of upper-case letters in the given string is: 0
Number of lower-case letters in the given string is: 10
Number of vowels in the given string is: 3
Number of consonants in the given string is: 7

You might also like