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 stringExplanationIn 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
SolutionPythonsentence = "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 JAVApublic 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#
|