TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to Count the Total Number of Words in a String

Program to Count the Total Number of Words in a String on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.

<< Back to PROGRAM

Program to count the total number of words in a string

Explanation

In this program, we need to count the words present in the string.

Beauty lies in the eyes of beholder

Total number of words present in the string is 7.

Algorithm

  1. Define a string.
  2. To counts the words present in the string, we will iterate through the string and count the spaces present in the string. As each word always ends with a space.
  3. If a string starts with a space, then we must not count the first space as it is not preceded by a word.
  4. To count the last word, we will increment the count by 1.

Solution

Python

sentence = "Beauty lies in the eyes of beholder";
wordCount = 0;
 
for i in range(0, len(sentence)-1):
    #Counts all the spaces present in the string
    #It doesn't include the first space as it won't be considered as a word
    if(sentence[i] == ' ' and sentence[i+1].isalpha() and (i > 0)):
        wordCount = wordCount + 1;
        

#To count the last word present in the string, increment wordCount by 1
wordCount = wordCount + 1;
 
#Displays the total number of words present in the given string
print("Total number of words in the given string: " + str(wordCount));

Output:

Total number of words in the given string: 7

C

#include <stdio.h>
#include <string.h>
 
int main()
{
    char sentence[] = "Beauty lies in the eyes of beholder";
    int wordCount = 0;
    
    for(int i = 0; i < strlen(sentence)-1; i++) {
        //Counts all the spaces present in the string
        //It doesn't include the first space as it won't be considered as a word
        if(sentence[i] == ' ' && isalpha(sentence[i+1]) && (i > 0)) {
            wordCount++;
        }
    }  
    //To count the last word present in the string, increment wordCount by 1
    wordCount++;
    
    //Displays the total number of words present in the given string
    printf("Total number of words in the given string: %d", wordCount);
 
    return 0;
} 

Output:

Total number of words in the given string: 7

JAVA

public class CountWords 
{
    public static void main(String[] args) {
        String sentence = "Beauty lies in the eyes of beholder";
        int wordCount = 0;
        
        for(int i = 0; i < sentence.length()-1; i++) {
            //Counts all the spaces present in the string
            //It doesn't include the first space as it won't be considered as a word
            if(sentence.charAt(i) == ' ' && Character.isLetter(sentence.charAt(i+1)) && (i > 0)) {
                wordCount++;
            }
        }
        //To count the last word present in the string, increment wordCount by 1
        wordCount++;
        
        //Displays the total number of words present in the given string
        System.out.println("Total number of words in the given string: " + wordCount);
    }
}

Output:

Total number of words in the given string: 7 

C#

using System;
                    
public class CountWords
{
    public static void Main()
    {
        String sentence = "Beauty lies in the eyes of beholder";
        int wordCount = 0;
        
        for(int i = 0; i < sentence.Length-1; i++) {
            //Counts all the spaces present in the string
            //It doesn't include the first space as it won't be considered as a word
            if(sentence[i] == ' ' && Char.IsLetter(sentence[i+1]) && (i > 0)) {
                wordCount++;
            }
        }
        //To count the last word present in the string, increment wordCount by 1
        wordCount++;
        
        //Displays the total number of words present in the given string
        Console.WriteLine("Total number of words in the given string: " + wordCount);
    }-
}           

Output:

Total number of words in the given string: 7

PHP

<!DOCTYPE html>
<html>
<body>
<?php
$sentence = "Beauty lies in the eyes of beholder";
$wordCount = 0;
 
for($i = 0; $i < strlen($sentence)-1; $i++) {
    //Counts all the spaces present in the string
    //It doesn't include the first space as it won't be considered as a word
    if($sentence[$i] == ' ' && ctype_alpha($sentence[$i+1]) && ($i > 0)) {
        $wordCount++;
    }
}
//To count the last word present in the string, increment wordCount by 1
$wordCount++;
 
//Displays the total number of words present in the given string
print("Total number of words in the given string: " . $wordCount);
?>
</body>
</html>

Output:

Total number of words in the given string: 7

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf