To count the number of words in a given string

This is the program to count number of words in the entered sentence.

Logic: The logic behind this program is checking if the character in the sentence is a ‘blank’ and with the conditions that the next is not a blank character, or current character position is not equal to length of the sentence. If so, variable ‘words’ is incremented by one as one new word found.

#include<stdio.h>
#include<conio.h>
void main()
{
char str[50];
int words = 0, len = 0, i;
clrscr();
printf(“\n\n\t ENTER A STRING…: “);
gets(str);
while(str[len]!=’\0′)
len++;
len–;
for(i=0;i<=len;i++)
{
if ( ( str[i] == ‘ ‘ && str[i+1] != ‘ ‘ ) || i == len )
words++;
}
printf(“\n\t NUMBER OF WORDS IN THE ABOVE SENTENCE IS…: %d”, words);
getch();
}
Download exe and source code here.

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

13 thoughts on “To count the number of words in a given string

  1. Why are you writing
    if ( ( str[i] == ‘ ‘ && str[i+1] != ‘ ‘ ) || i == len )
    PLEASE Explain………

  2. public class StringDemo {
    public static void main(String[] ar){
    String str=” HAI hEllO HOw arE You hai bga jaja abjblkds jksdnsd dnjd dndn iddwd dbdb cjb “;
    int count=0;
    int a=0,e=0,i=0,o=0,u=0,b=0,c=0,d=0;
    System.out.println(str);
    //System.out.println(“No. of Characters =”+str.length());
    str=str.trim();
    //System.out.println(“No. of Characters =”+str.length());
    for(int j=0,k=1;j<str.length();j++,k++){
    if(str.charAt(j)=='a'||str.charAt(j)=='A'){
    a++;
    c++;
    }
    else if(str.charAt(j)=='e'||str.charAt(j)=='E'){
    e++;
    c++;
    }
    else if(str.charAt(j)=='i'||str.charAt(j)=='I'){
    i++;
    c++;
    }
    else if(str.charAt(j)=='o'||str.charAt(j)=='O'){
    o++;
    c++;
    }
    else if(str.charAt(j)=='u'||str.charAt(j)=='U'){
    u++;
    c++;
    }
    else{
    b++;
    }
    if(str.charAt(j)==' '){
    count++;
    }
    if((str.charAt(k-1)!=' '&& k<str.length())&& (str.charAt(k)==' ')){
    d++;

    }
    }
    System.out.println("No. of Characters (without Spaces)="+(str.length()-count));
    System.out.println("No. of spaces: "+count);
    // System.out.println("No. of Words: "+(count+1));
    System.out.println("No. of Words: "+(d+1));
    System.out.println("No. of consonants and Symbols(if any): "+(b-count));
    System.out.println("No. of vowel's: "+c);
    System.out.println("No. of 'a's: "+a);
    System.out.println("No. of 'e's: "+e);
    System.out.println("No. of 'i's: "+i);
    System.out.println("No. of 'o's: "+o);
    System.out.println("No. of 'u's: "+u);

    }
    }

    OUTPUT :
    HAI hEllO HOw arE You hai bga jaja abjblkds jksdnsd dnjd dndn iddwd dbdb cjb
    No. of Characters (without Spaces)=62
    No. of spaces: 31
    No. of Words: 15
    No. of consonants and Symbols(if any): 46
    No. of vowel's: 16
    No. of 'a's: 7
    No. of 'e's: 2
    No. of 'i's: 3
    No. of 'o's: 3
    No. of 'u's: 1

  3. in above program i place spaces betweeen evry word like nearly 5 spaces between each word so i write like that above without spaces….pls understand perfectly ..!!!!

  4. if input is ” ” (only space) it is showing as output = 1, but it will 0. waiting for your reply with your help. thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in