StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Program #29 (U3): Count Words in a Sentence
Download
  1. C Programming
  2. C Programming Practice
Program #28 (U3): Count Character Categories in a String : Program #30 (U3): Palindrome String Check (case-insensitive)
C Programming Practice

Task: Read a line and count transitions from non-word to word characters (letters or digits) to handle multiple spaces. Print the total word count.

 
#include <stdio.h>

#include <ctype.h>

 
int main(void) {

    char s[512];

    int c, i=0, in_word=0, words=0;

 
    printf("Enter a sentence: ");

    while ((c=getchar())!='\n' && c!=EOF) { if(i<511) s[i++]=(char)c; }

    s[i]='\0';

 
    for (i=0; s[i]; ++i) {

        if (isalnum((unsigned char)s[i])) {

            if (!in_word) { in_word = 1; ++words; }

        } else {

            in_word = 0;

        }

    }

 
    printf("Word count = %d\n", words);

    return 0;

}

 

 

Program #28 (U3): Count Character Categories in a String Program #30 (U3): Palindrome String Check (case-insensitive)
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website