Program to count the number of words in a sentence
Last Updated :
18 Jan, 2024
Write a program to count the number of words in a given sentence. A word is defined as a sequence of characters separated by spaces.
Examples:
Input: “The quick brown fox”
Output: 4
Explanation: The sentence has four words – “The”, “quick”, “brown”, “fox”.
Input: “The quick brown fox”
Output: 4
Explanation: The sentence has four words – “The”, “quick”, “brown”, “fox”.
Approach: To solve the problem, follow the below idea:
Iterate through each character in the sentence and count the number of spaces to identify the separation between words. The total count of spaces plus 1 gives the number of words.
Step-by-step algorithm:
- Initialize a variable
wordCount
to 0.
- Iterate through each character in the sentence.
- Check if the current character is a space.
- If a space is found, increment the
wordCount
.
- After the loop, add 1 to
wordCount
to account for the last word.
- Output the final value of
wordCount
.
Below is the implementation of the algorithm:
C++
#include <iostream>
using namespace std;
int countWords( const string& sentence)
{
int count = 1;
for ( char character : sentence) {
if (character == ' ' ) {
count++;
}
}
return count;
}
int main()
{
string sentence = "Programming is fun and challenging" ;
cout << "Number of words: " << countWords(sentence)
<< endl;
return 0;
}
|
C
#include <stdio.h>
int countWords( char sentence[]) {
int count = 1;
for ( int i = 0; sentence[i] != '\0' ; ++i) {
if (sentence[i] == ' ' ) {
count++;
}
}
return count;
}
int main() {
char sentence[] = "Programming is fun and challenging" ;
printf ( "Number of words: %d\n" , countWords(sentence));
return 0;
}
|
Java
public class WordCounter {
static int countWords(String sentence) {
int count = 1 ;
for ( int i = 0 ; i < sentence.length(); ++i) {
if (sentence.charAt(i) == ' ' ) {
count++;
}
}
return count;
}
public static void main(String[] args) {
String sentence = "Programming is fun and challenging" ;
System.out.println( "Number of words: " + countWords(sentence));
}
}
|
Python3
def count_words(sentence):
count = 1
for char in sentence:
if char = = ' ' :
count + = 1
return count
sentence = "Programming is fun and challenging"
print ( "Number of words:" , count_words(sentence))
|
C#
using System;
class Program {
static int CountWords( string sentence)
{
int count = 1;
foreach ( char character in sentence)
{
if (character == ' ' ) {
count++;
}
}
return count;
}
static void Main()
{
string sentence
= "Programming is fun and challenging" ;
Console.WriteLine( "Number of words: "
+ CountWords(sentence));
}
}
|
Javascript
function countWords(sentence) {
let count = 1;
for (let char of sentence) {
if (char === ' ' ) {
count++;
}
}
return count;
}
let sentence = "Programming is fun and challenging" ;
console.log( "Number of words:" , countWords(sentence));
|
Output
Number of words: 5
Time Complexity: O(N) where N is the length of the sentence.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...