[Go to site: main page, start]

0% found this document useful (0 votes)
10 views4 pages

Stringprograms

The document contains a series of programming exercises focused on string manipulation in Python. Each exercise includes a specific task such as counting vowels, reversing strings, checking for palindromes, and identifying special characters. The document provides code snippets for each task, demonstrating various string operations and logic.

Uploaded by

shuklanikidha
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)
10 views4 pages

Stringprograms

The document contains a series of programming exercises focused on string manipulation in Python. Each exercise includes a specific task such as counting vowels, reversing strings, checking for palindromes, and identifying special characters. The document provides code snippets for each task, demonstrating various string operations and logic.

Uploaded by

shuklanikidha
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

Programming of String

1. Write a program to count the number of vowels in a given string. Your program should do the following: [2025]
i) Define a string
ii) Traverse the string and count the vowels using the loop
iii) Print the total count of vowels

s=input("Enter a sentence")
s=[Link]()
c=0
v="aeiou"
for i in s:
if i in v: # to check i in v
c=c+1
print("Number of vowels=",c)
2. Write a program to input a string, reverse it and check it is palindrome or not.

s=input("Enter a word")
s=[Link]()
r=s[::-1] # to reverse the string
if s==r:
print(s, "is palindrome")
else:
print(s, "is not palindrome")

3. Write a program to input a sentence, count number of spaces and words.

s=input("Enter a sentence")
c=0
for i in s:
if i==' ': # to check space
c=c+1
print("Number of spaces=",c)
print("Number of words=",c+1)

4. Write a program to input a sentence and print all the words in separate line.

s=input("Enter a sentence")
Lst=[Link]() # to store words of string into list
for i in Lst:
print(i)

5. Write a program to accept a string and print the number of digits, alphabets and special characters in the string.
Input: “KAPILDEV@83”
Output: Number of digits=2
Number of Alphabets=8
Number of Special characters=1
s=input("Enter a sentence")
s=[Link]()
cd=0
ca=0
cs=0
for i in s:
if [Link](): # to check alphabet
ca=ca+1
elif [Link](): # to check digits
cd+=1
else:
cs+=1
print("Number of digits=",cd)
print("Number of alphabets=",ca)
print("Number of special characters=",cs)
6. Define a class to accept a String and print if it is a Super String or not. A String is Super if the number of
uppercase letters are equal to the number of lower case letters.
Ex: “COmmITmeNt”
Number of Uppercase letters – 5
Number of Lowercase letters – 5
String is a Super String

s=input("Ente a string")
c=0
d=0
for i in s:
if [Link](): # to check uppercase
c+=1
elif [Link](): # to check lowercase
d+=1
if c==d:
print(s,"is super string")
else:
print(s,"is not super string")

7. Write a program to input a sentence and print the longest word and its number of characters.
Input: India is my country
Output: country
No. of characters=7

s=input("Ente a string")
lst=[Link]() # to store words of string into list
max=""
for i in lst:
if len(i)>len(max):
max=i
print("Longest word",max)
print("number of charactes=",len(max))
8. Write a program to input a sentence and print the smallest word and its number of characters.
Input: India is a great country
Output: a
No. of characters=1

s=input("Ente a string")
lst=[Link]() # to store words of string into list
min=s
for i in lst:
if len(i)<len(min):
min=i
print("smallest word",min)
print("number of charactes=",len(min))

9. Write a program to accept a sentence and word from user. The program should count and print the number of
times the word appears in the sentence.
Input: the digit and the number is checked with the another digit
Input word to check: the
Output: frequency of word the=3

s=input("Ente a string")
w=input("Enter a word")
lst=[Link]() # stored word of string into list
c=[Link](w)
print("frequency of word=",c)

10. Write a program to input a string, convert the upper case letter to lower case and lower case to upper case.

Input: coMpuTeR
Output: COmPUtEr

s=input("Ente a string")
w="" # Variable has null value
for i in s:
if [Link]():
i=[Link]()
elif [Link]:
i=[Link]()
w=w+i
print("Resultant word",w)

11. Write a program to input a string, count the number of vowels, consonants, digits, spaces and special characters.

s=input("Ente a string")
s=[Link]()
v="AEIOU"
cv=0 # to count vowel
cc=0 # to count consonant
cd=0 # to count digit
csp=0 # to count spaces
csc=0 # to count special character
for i in s:
if [Link]():
if i in v:
cv+=1
else:
cc+=1
elif [Link]():
cd+=1
elif i==' ':
csp+=1
else:
csc+=1
print("Number of vowels=",cv)
print("Number of consonants=",cc)
print("Number of digits=",cd)
print("Number of spaces=",csp)
print("Number of special characters=",csc)

12. write a program to accept a word and print it is Palindrome or only Special word.
Special word are those which starts and ends with the same letter.
Ex: EXISTENCE, WINDOW
Palindrome words are those which read the same from left and right. Ex: MALAYALAM, MADAM

s=input("Ente a string")
s=[Link]()
r=s[: : -1] # to reverse string
if r==s:
print(s,"is a palindrome")
elif s[0]==s[len(s)-1]: # to compare first and last character of word
print(s, "is a special word")
else:
print("Neither palindrome nor special")

You might also like