word count

This is a discussion on word count within the C Programming forums, part of the General Programming Boards category; Hi, I am very new to programming and just started learning C. I have a program that asks the user ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    Question word count

    Hi,
    I am very new to programming and just started learning C. I have a program that asks the user to input text. The user allowed to enter any ASCII characters they want, it will be terminated when the user enters ctl+Z. In the program I need to find out how many letters in the user's input and the number of words. Only the letters from 'A' to 'Z' and 'a' to 'z' will be counted as words and letters. Also, when the user enters I'm instead of 'I am', it should be counted as two words. So, I did write a program, figured out how to count the letters. Tried to do the word count in different ways. I couldn't get it to work. Any help/hints will be very much appreciated. Thank you in advance. Here is my code:

    # include<stdio.h>

    main()
    {
    int ch, letter = 0, word = 0, inword = 0;

    printf("Enter lines of text. To terminate, type control-Z: ");
    ch = getchar();
    //inword = 0;

    while(ch!= EOF)
    {
    if((ch>='A' && ch<= 'Z') || (ch>='a' && ch<='z'))
    {
    letter++;
    ch = getchar();
    }
    else if(!(ch>='A' && ch<= 'Z') || (ch>='a' && ch<='z'))
    {
    //if(inword >= ((ch>='A' && ch<= 'Z') || (ch>='a' && ch<='z')))
    //{word++;}
    ch = getchar();
    }
    //inword = ch;
    }
    printf("\n Letters: %d",letter);
    //printf("\n Words: %d", word);

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    I have not changed the code. I have just made it easier to read.

    Code:
    # include<stdio.h> 
    
    int main(void) 
    { 
     int ch, letter = 0, word = 0, inword = 0; 
     printf("Enter lines of text. To terminate, type control-Z: "); 
     ch = getchar(); 
     //inword = 0; 
     while(ch!= EOF) 
     { 
      if((ch>='A' && ch<= 'Z') || (ch>='a' && ch<='z')) 
      { 
       letter++; 
       ch = getchar(); 
      } 
      else if(!(ch>='A' && ch<= 'Z') || (ch>='a' && ch<='z')) 
      { 
       //if(inword >= ((ch>='A' && ch<= 'Z') || (ch>='a' && ch<='z'))) 
       //{word++;} 
       ch = getchar(); 
      } 
      //inword = ch; 
     } 
     printf("\n Letters: %d",letter); 
     //printf("\n Words: %d", word); 
     return 0; 
    }

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    You start with this loop for reading each character

    Code:
    while ( (ch = getchar()) != EOF ) {
        // do stuff
    }

    > if((ch>='A' && ch<= 'Z') || (ch>='a' && ch<='z'))
    This is good, but you need to add some more stuff
    Call the above test is_letter

    Then you can think about, for instance,
    Code:
    if ( is_letter && !inword ) {
        in_word = 1;
        word++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bintree and count (withouth using template)?
    By cubimongoloid in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2009, 06:22 AM
  2. word count
    By unity_1985 in forum C Programming
    Replies: 3
    Last Post: 07-29-2007, 10:34 AM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. word count troubles
    By Hoser83 in forum C Programming
    Replies: 13
    Last Post: 02-09-2006, 08:12 AM
  5. Replies: 5
    Last Post: 09-28-2004, 12:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21