String

C program to count the total number of words in the given string

C program to count the total number of words in the given string

In this article, we will discuss the concept of C program to count the total number of words in the given string

In this post, we are going to learn how to count the total number of words in the given string in C  programming language

Count words of the string

Program to count the total number of words using for loop

The program allows the user to enter a string  thereafter It counts the total words of the string using for loop in C language

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[100];
    int count,i,words=1;

    printf("Enter a string for count words\n");
    gets(str);//store the string vlue gets from user
    count=strlen(str);
    for(i=0; i<count; i++){
        if(str[i]!=' ' && str[i+1]==' ')
            words=words+1;
    }
    printf("Total words of the given string: %d ",words);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter a string for count words
C is a procedural language
Total words of the given string: 5

 

Approach

  1. Declare a character array as char str[100];
  2. Declare and initialize an integer variable as int i ,count,words=0;
  3. The user asked to enter a string to count words
  4. Calculate the length of the given string using strlen() function and assign the length to count variable
  5. A for-loop is used to count the total words of the given string
  6. It is initialized as i=0, and checks condition whether i<count, and executes the loop until the given condition becomes true
  7. Use an if condition to test if(str[i]!=’ ‘ && str[i+1]==’ ‘), if it is true,  words becomes words + 1(words=words+1)
  8. Finally, the program displays the total number of the words using the words variable

 

Program to count the total number of words using while loop

The program allows the user to enter a string  thereafter It counts the words of the string using while loop in C  language

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[100];
    int count,i,words=1;

    printf("Enter a string for count words\n");
    gets(str);
    count=strlen(str);
    i=0;
    while(i<count){
        if(str[i]!=' ' && str[i+1]==' ')
            words=words+1;
             i++;
    }
    printf("Total words of the given string: %d ",words);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter a string for count words
C is supporting structured programming
Total words of the given string: 5

 

Approach

  1. Declare a character array as char str[100];
  2. Declare and initialize an integer variable as int i ,count,words=0;
  3. The user asked to enter a string to count words
  4. Calculate the length of the given string using strlen() function and assign the length to count variable
  5. A while-loop is used to count the total words of the given string
  6. It is initialized as i=0, and checks condition whether i<count, and executes the loop until the given condition becomes true
  7. Use an if condition to test if(str[i]!=’ ‘ && str[i+1]==’ ‘), if it is true,  words becomes words + 1(words=words+1)
  8. i is incremented by 1
  9. Finally, the program displays the total number of the words using the words variable

 

Program to count the total number of words using do-while loop

The program allows the user to enter a string  thereafter It counts the words of the string using do-while loop in C language

Program 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[100];
    int count,i,words=1;

    printf("Enter a string for count words\n");
    gets(str);
    count=strlen(str);
    i=0;
    do{
        if(str[i]!=' ' && str[i+1]==' ')
            words=words+1;
             i++;
    }while(i<=count);
    printf("Total words of the given string: %d ",words);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter a string for count words
C language is not supported OOP concept
Total words of the given string: 7

 

Approach

  1. Declare a character array as char str[100];
  2. Declare and initialize an integer variable as int i ,count,words=0;
  3. The user asked to enter a string to count words
  4. Calculate the length of the given string using strlen() function and assign the length to count variable
  5. A do-while loop is used to count the total words of the given string
  6. It is initialized as i=0, and checks condition whether i<count, and executes the loop until the given condition becomes true
  7. Use an if condition to test if(str[i]!=’ ‘ && str[i+1]==’ ‘), if it is true,  words becomes words + 1(words=words+1)
  8. i is incremented by 1(i=i+1)
  9. Finally, the program displays the total number of the words using the words variable

 

Suggested for you

for loop in C language

While loop in C language

do-while loop in C language

 

Similar post

Java program to count the total number of characters in the given string

C++ program to count the total number of characters in the given string

C program to count the total number of characters in the given string

Python program to count the total number of characters in the given string

 

Python program to count the total number of words in the given string

Java program to count the total number of words in the given string

C++ program to count the total number of words in the given string

 

C++ program to count the total number of words in the given string
Python program to count the total number of words in the given string
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago