SATISH KUMAR’s Post

Day 35 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to count the number of words in a sentence without using the split() function. The goal was to understand how word detection works internally by scanning characters one by one. What the program does: • Takes a sentence as input • Iterates through each character • Detects transitions between spaces and words • Counts words manually without using split() • Handles punctuation and extra spaces How the logic works: •Two variables are initialized: – word_count to track the number of words – in_word to track whether we are currently inside a word •The program loops through each character in the sentence •If the character is alphanumeric (isalnum()): – It indicates part of a word – If we were not previously inside a word, the word count increases •If the character is not alphanumeric: – It marks the end of a word – in_word becomes False •This continues until the entire sentence is processed Example: Input: "Hello world! How are you?" Output: Word count → 5 Another example: Input: "This is a test sentence with extra spaces." Output: Word count → 8 Another example: Input: "One-two three." Output: Word count → 3 Why this approach is useful: – Shows how word counting works internally – Avoids built-in shortcuts – Handles punctuation and multiple spaces – Time Complexity: O(n) Key learnings from Day 35: – Parsing strings character by character – Using boolean flags to track state – Understanding how word boundaries work – Strengthening string processing logic #100DaysOfCode #Day35 #Python #PythonProgramming #StringProcessing #Algorithms #ProblemSolving #CodingPractice #LearnByDoing #ComputerScience #ProgrammingJourney #DeveloperGrowth #BTech #CSE #AIandML #VITBhopal #TechJourney

  • text

To view or add a comment, sign in

Explore content categories