Skip to content
Programming Problems · Q1

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

West Bengal WbchseTextbookSubjective· 5mImportance★★★★★est
69% · 9/13 Questions
✓ Free question

Read the line with input(), then classify each character in a single loop using isalpha() and isdigit(); total = len(text), and words = pieces produced by splitting on single spaces.

Idea. input() reads characters until Enter is pressed — that gives us the text. Then this is a pure counting problem: walk the string once and put every character into exactly one bucket — alphabet, digit, or special symbol (we treat the space as a word separator, not a special symbol). Words need no second pass: since the problem guarantees each word is separated by one space, splitting on the space counts them.

Algorithm

  1. Read text with input().
  2. Set counters alphabets = digits = special = 0.
  3. For each character: isalpha() → alphabet; else isdigit() → digit; else if it is not a space → special symbol.
  4. Total characters = len(text) (spaces included automatically).
  5. Words = len(text.split(' ')) — with one space between words, this equals number of words (guard the empty string with 0).
text = input("Enter the text: ")

alphabets = digits = special = 0
for ch in text:
    if ch.isalpha():
        alphabets += 1
    elif ch.isdigit():
        digits += 1
    elif ch != ' ':
        special += 1

words = len(text.split(' ')) if text else 0

print("Total characters (with spaces):", len(text))
print("Total alphabets:", alphabets)
print("Total digits:", digits)
print("Total special symbols:", special)
print("Total words:", words)

Sample run — count it by hand to verify: "Hello World 123 @#" has 5+1+5+1+3+1+2 = 18 characters; letters H-e-l-l-o-W-o-r-l-d = 10; digits 1-2-3 = 3; specials @ and # = 2; pieces "Hello"/"World"/"123"/"@#" = 4 words.

Enter the text: Hello World 123 @#
Total characters (with spaces): 18
Total alphabets: 10
Total digits: 3
Total special symbols: 2
Total words: 4

The loop is O(n) — one look at each character.

Watch out

Order matters in the if-elif chain: test isalpha() before isdigit() before "everything else", and remember to exclude the space from special symbols — otherwise every separator inflates that count. Also "123" counts as a word here (a space-separated token), which is exactly what the question's assumption implies.

✓Final answer

The program reads the line, counts len(text) total characters, classifies each character via isalpha()/isdigit()/otherwise-special (spaces skipped), and counts words with text.split(' '); e.g. input "Hello World 123 @#" gives 18 characters, 10 alphabets, 3 digits, 2 special symbols and 4 words.

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.