Q1: Write a program to input line(s) of text from the user until enter is pressed. Count the total number of characters in the text (including white spaces),total number of alphabets, total number of digits, total number of special symbols and total number of words in the given text. (Assume that each word is separated by one space).
#Solution 1
text = input("Enter the text: ")
ch = 0
alph = 0
digits = 0
special = 0
words = 0
for i in text:
ch+=1
if i.isdigit() == True:
digits += 1
elif i.isalpha() == True:
alph += 1
elif i.isspace() == True:
words += 1
else:
special += 1 #since characters can either be digits, alphabets, whitespace or special characters
print('Characters: ' + str(ch))
print('Alphabets: ' + str(alph))
print('Digits: ' + str(digits))
print('Special Symbols: ' + str(special))
print('Words: ' + str(words + 1)) #We add one since by counting spaces, we ignore one word
#Solution 2
text = input("Enter the text: ")
ch = 0
alph = 0
digits = 0
special = 0
words = 0
for i in text:
ch+=1
if i in "01234567890":
digits += 1
elif i in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
alph += 1
elif i == " ":
words += 1
else:
special += 1 #since characters can either be digits, alphabets, whitespace or special characters
print('Characters: ' + str(ch))
print('Alphabets: ' + str(alph))
print('Digits: ' + str(digits))
print('Special Symbols: ' + str(special))
print('Words: ' + str(words + 1)) #We add one since by counting spaces, we ignore one word
Q2: Write a user defined function to convert a string with more than one word into title case string where string is passed as parameter. (Title case means that the first letter of each word is capitalised)
def title_func():
word = input("Enter a string: ")
new = ""
for i in range(len(word)):
if i == 0:
new+=word[0].upper()
elif word[i-1] == " ":
new += word[i].upper()
else:
new += word[i]
return new
title_func()
Q3: Write a function deleteChar() which takes two parameters one is a string and other is a character. The function should create a new string after deleting all occurrences of the character from the string and return the new string.
def deleteChar():
string = input("Enter string: ")
ch = input("Enter character to be deleted: ")
new = ""
for i in string:
if i == ch:
continue
else:
new += i
return new
deleteChar()
Q4: Input a string having some digits. Write a function to return the sum of digits present in this string.
def sumdigits():
string = input("Enter string: ")
total = 0
for i in string:
if i.isdigit() == True:
total += int(i)
else:
continue
return total
sumdigits()
Q5: Write a function that takes a sentence as an input parameter where each word in the sentence is separated by a space. The function should replace each blank with a hyphen and then return the modified sentence.
def hyphenate():
string = input("Enter string: ")
new = ""
for ch in string:
if ch == " ":
new += "-"
else:
new += ch
return new
hyphenate()
For more tutorials and solutions check out the rest of our website! Q-Programming CBSE Cs. Follow us on Instagram for more content!