C

Write a Program To Count Number of Words in String in C

In this tutorial, we are going to see how to write a program to count the number of words in a string in C. The following C program asks the user to enter a String then count the number of words in this string and print it on the screen. For example, if the user enters the following string “Welcome to StackHowTo” the program will print “The total number of words in this string is = 3”.
100-multiple-choice-questions-in-c-programming100 Multiple Choice Questions In C Programming – Part 1This collection of 100 Multiple Choice Questions and Answers (MCQs) In C Programming : Quizzes & Practice Tests with Answer focuses on “C Programming”.  …Read More

Write a Program To Count Number of Words in String in C
#include <stdio.h>
#include <string.h>
 
int main()
{
  char str[100];
  int i, count = 1;
   
  printf("Enter a string: ");
  gets(str);

  for(i = 0; str[i] != '\0'; i++)
  {
    if(str[i] == ' ' || str[i] == '\t' || str[i] == '\n')
    {
      count++;  
    } 
  }  
  printf("The total number of words in this string is = %d", count);
    
  return 0;
}

Output:

Enter a string: Welcome to StackHowTo
The total number of words in this string is = 3

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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