Count Words in Sentence in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
#include <string.h>

int main() {
    char str[200];
    int i, count = 1;
    printf("Enter a sentence: ");
    fgets(str, sizeof(str), stdin);

    for(i = 0; str[i] != '\0'; i++) {
        if(str[i] == ' ' && str[i+1] != ' ' && str[i+1] != '\0')
            count++;
    }
    printf("Word count: %d\n", count);
    return 0;
}

C Output

Input:
Hello world this is C

Output:
Word count: 5



C++ Program

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    int count = 1;
    cout << "Enter a sentence: ";
    getline(cin, str);

    for(int i=0; i<str.size(); i++) {
        if(str[i] == ' ' && str[i+1] != ' ' && str[i+1] != '\0')
            count++;
    }
    cout << "Word count: " << count << endl;
    return 0;
}

C++ Output

Input:
AI makes coding easy

Output:
Word count: 4



JAVA Program

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String str = sc.nextLine();
        sc.close();

        String[] words = str.trim().split("\\s+");
        System.out.println("Word count: " + words.length);
    }
}

JAVA Output

Input:
Java is platform independent

Output:
Word count: 4



Python Program

sentence = input("Enter a sentence: ")
words = sentence.strip().split()
print("Word count:", len(words))

Python Output

Input:
Python is fun to learn

Output:
Word count: 5



In-Depth Explanation
Example
When you type "Hello world this is C", the program reads character by character. Whenever it detects a space and a non-space character, it detects a new word. That's why the result becomes 5, as the words are: "Hello", "world", "this", "is", "C".

Real-Life Analogy
Imagine words such as passengers on a bus. Spaces are equivalent to the doors between them. To count the passengers, you simply observe the spaces that separate them. Each set of letters between spaces constitutes a word. If you had just one long passenger without spaces, you'd only have one word.

Why It Matters
Word counting itself can appear to be straightforward, yet it is the base of numerous practical applications. Search engines such as Google must count words to parse queries. Word processing software such as Microsoft Word will display the word count of your document at once. Even social networks like Twitter and Instagram restrict messages by character or word number, so learning how to count words is crucial in text-based systems.

Programming Insights
In C and C++, we handle traversal of characters and spaces explicitly, whereas in Java and Python, there are higher-level functions like split() that explicitly split a string into words. This observation points to two significant programming philosophies: low-level control (C/C++) vs. high-level convenience (Java/Python). As a student, this makes you understand how abstraction is implemented in languages.

Interview Use Case
Interviewers adore word-counting exercises since they probe string manipulation, space handling, whitespace trimming of excess whitespaces, and boundary cases such as multiple spaces, null input, or punctuation. An ideal candidate not only codes a program but also describes how to process challenging cases.

Real-World Applications
Word counting is not only academic—plagiarism checkers, chat filters, text editors, SEO tools, and data analysis all use it. SEO professionals, for instance, need to know how many words are in an article in order to optimize for Google ranking. There are apps such as Grammarly and Notion that generate live word counts for productivity and writing objectives.

SEO-Optimized Learning Paragraph
Word counting in a sentence is one of the most frequent string manipulation tasks in computer programming. It makes the fundamentals of strings, loops, and conditionals crystal clear while demonstrating real-world usage in SEO, blogging, search engines, and text analysis. By understanding word counting in C, C++, Java, and Python, students get an idea of how computers work with human language, which is the backbone of natural language processing (NLP) and artificial intelligence. This idea also enhances problem-solving skills, enabling students to prepare for coding interviews and actual projects where text processing is crucial.