C Program
#include<stdio.h> #include<string.h> int main() { char s[200]; gets(s); int c = 0; for(int i=0; s[i]; i++) if(s[i]!=' ' && (i==0 || s[i-1]==' ')) c++; printf("Words=%d", c); }
C Output
Input: Hello world this is C Output: Words=5
C++ Program
#include<iostream> using namespace std; int main() { string s; getline(cin, s); int c=0; for(int i=0; i<s.size(); i++) if(s[i]!=' ' && (i==0 || s[i-1]==' ')) c++; cout << "Words=" << c; }
C++ Output
Input: C++ is fast and powerful Output: Words=5
JAVA Program
import java.util.*; class W { public static void main(String[] a) { String s = new Scanner(System.in).nextLine(); System.out.print("Words=" + s.trim().split("\\s+").length); } }
JAVA Output
Input: Java is a popular language Output: Words=5
Python Program
s = input() print("Words =", len(s.split()))
Python Output
Input: Python is great for text processing Output: Words = 6
In-Depth Learning – Full Concept in Paragraphs
What is Counting Words?
Word counting for a sentence involves counting the number of individual space-delimited pieces of text (known as tokens or words) within the sentence. A word is any unbroken chain of characters without spaces. This simple string operation is the foundation of language processing, text analysis, and document parsing.
How the Code Works
In all the languages, a word is taken to begin when:
The current character is not a space and
The last character was a space (or it's the first ever character)
So, scanning the string from left to right using this rule, we can get the count of each word. This is achieved without the use of additional arrays or libraries in C/C++.
In Java, we employ split("agues+") which splits the string by one or more spaces (this takes care of multiple spaces, tabs, etc.).
In Python, split() called without arguments takes care of all whitespace and consecutive whitespaces automatically, returning a list of words whose length is used to determine the word count.
Example
Input:
arduino
" Hello world! This is ChatGPT. "
Cleaned:
["Hello", "world!", "This", "is", "ChatGPT."]
Output:
Words = 5
Even when there are additional spaces, the logic still holds true.
Real-Life Analogy
Think of writing a sentence in Microsoft Word. When you switch on word count, it does not count the spaces, but rather single blocks of text (tokens) divided by whitespace. That is precisely what this program does.
Also, think of sending an SMS — you are usually billed per number of words, not spaces. Counting actual text units is crucial!
Where and When Is It Used?
Word counting is applied in:
Text editors (Word, Google Docs)
Reading time estimators
Essay length validators
Language processing tools
Speech-to-text programs
Chatbots and AI training
Also utilized in coding interviews to check string traversal, parsing, and text logic ability.
Time and Space Complexity
Operation Complexity
Time O(n) – where n is string length
Space O(1) (C/C++) or O(n) (Java/Python because of split or tokenization)
One of the most scalable and efficient problems for learning string manipulation.
Python-Specific Tip
In Python:
len(s.split())
Works with multiple spaces, tabs, and newline characters. It's solid, lightweight, and interview-proof.
SEO-Optimized Natural Paragraph for Ranking
Need to count words of a sentence in C, C++, Java, or Python? This tutorial provides you with the shortest and most minimal code to count words in any given string. Word counting is among the most basic string manipulation operations in programming and text analysis. No matter what software you are creating to read documents, interpret user input, or ready data for natural language processing (NLP), word counting assists you in sanitizing, measuring, and assessing text data effectively. This example employs straightforward character logic and robust built-in techniques to word count, even with additional or irregular spacing.
Social Plugin