## ❮ Back

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).

In [1]:
#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


Enter the text: Welcome to Q-Programming! Today is 18.08.20
Characters: 43
Alphabets: 28
Digits: 6
Special Symbols: 4
Words: 6

In [3]:
#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

Enter the text: Welcome to Q-Programming! Today is 18.08.20
Characters: 43
Alphabets: 28
Digits: 6
Special Symbols: 4
Words: 6


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)

In [6]:
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

In [7]:
title_func()

Enter a string: hello and welcome to q-programming!

Out[7]:
'Hello And Welcome To Q-programming!'

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.

In [10]:
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


In [12]:
deleteChar()

Enter string: Hello And Welcome To Q-programming!
Enter character to be deleted: e

Out[12]:
'Hllo And Wlcom To Q-programming!'

Q4: Input a string having some digits. Write a function to return the sum of digits present in this string.

In [19]:
def sumdigits():
string = input("Enter string: ")
total = 0
for i in string:
if i.isdigit() == True:
total += int(i)
else:
continue

In [20]:
sumdigits()

Enter string: fbeb34536jjg445

Out[20]:
34

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.

In [24]:
def hyphenate():
string = input("Enter string: ")
new = ""
for ch in string:
if ch == " ":
new += "-"
else:
new += ch
return new

In [25]:
hyphenate()

Enter string: Hello And Welcome To Q-programming!

Out[25]:
'Hello-And-Welcome-To-Q-programming!'

For more tutorials and solutions check out the rest of our website! Q-Programming CBSE Cs. Follow us on Instagram for more content!