Thread: help concering counting of words in one sentc..

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    7

    Question help concering counting of words in one sentc..

    Hi Again
    i got the following prog so far.
    An example of how it works is as follow

    Enter a paragraph with less than 99 characters.

    Hi there. How are you? I guess you are fine.

    the number of sentences in the paragraph are: 3
    the number of words in the paragraph are: 10


    But i would like to know how can i count the number of words in each sentence, instead of in the entire paragraph.

    Please help
    Thank You.
    Code:
    #include <stdio.h>
    #define max_char 100
    
    main(){
     
    char   para[max_char];
     int     len_sent,i,count,count_sent,illegal,sentence;
    
    illegal = sizeof(para)-1;
    
    printf("Please enter a paragraph with less than %d characters\n",illegal);
    
    fflush(stdin);
    
    fgets(para,max_char,stdin);
    
    len_sent = strlen(para);
    
      count_sent=0;
      for (i=0; i<len_sent; i++) {
         if (para[i]== '.' || para[i]=='?' || para[i]=='!')
         count_sent++;
      }
    
    printf("the number of sentences are: %d\n", count_sent );
    
     count=1;
     for(i=0; i<len_sent; i++) {
       if (para[i] == ' ')
       count++;
     }
    
    printf("The number of words in the paragraph are %d\n",count);
    
    }

    Code tags added by Hammer

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    First up, 2 things, please don't start new threads on the same topic. Your old one has 2 replies on it, maybe they will help you. And please use code tags when posting code. See my signature for an example and a link.


    Now, why not merge the two loops you've created into one. Count the number of words as you go through the array, and when you spot the end of a sentence, simply write out your message saying "Words in sentence 1 = 4" (or whatever). At this point, reset the word counter to 0 ready for the next sentence.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 12-31-2007, 02:32 PM
  2. Counting Number of Words in a Text File Using C
    By wvu2005 in forum C Programming
    Replies: 16
    Last Post: 09-27-2005, 11:45 AM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. counting words in string
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 05-30-2002, 04:10 AM