1.

Write a program to count the number of characters, words and lines in a given string?

Answer»

string = input (“Enter string:”)

char = 0

word = 0

line = 0

for i in string:

char = char + 1

if (i = = “):

word = word + 1

elif (i = = ‘ \n’):

line = line +1

print (“Number of words in the string: “)

print (word)

print (“Number of characters in the string: “)

print (char)

print (“Number of lines in the string: “)

print (line)

Output:

Enter string: welcome to learning python

Number of words in the string : 4

Number of characters in the string : 26

Number of lines in the string : 1



Discussion

No Comment Found