Word count program

This is a discussion on Word count program within the C Programming forums, part of the General Programming Boards category; I'm trying to create a program in which a user inputs a string up to 100 characters, and the program ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    7

    Word count program

    I'm trying to create a program in which a user inputs a string up to 100 characters, and the program prints the number of words, and the number of words that start with a capital letter. I have it displaying the correct number of words, but not words that start with a capital letter. Any help would be appreciated.

    Code:
    #include <stdio.h>
    
    
    void sub(char mystring[100], int *word_count, int *cap_words, int *histogram)
    {
            int num_of_words, j, inside_a_word=0, word_cap=0;
            num_of_words = 0;
    
    		// Calulates star of words with capital letters. 
    		if ( mystring[0]<=90 && mystring[0]>=65)
    				{
    					word_cap=1;
    				}
    
    		// Calculates number of words. 
            for (j=0; j<100; j++)
            {
                    if(mystring[j] == ' ' || mystring[j] == 0)
                    {
                            if (inside_a_word == 1)
                            {
                                    num_of_words = num_of_words + 1;
                            }
                            inside_a_word = 0;
                    }
                    else
                    {
                            inside_a_word = 1;
                    }
    
    		// Calculates words that start with capital letters. 
    				
    				
    
    				if (mystring[j]== ' ')
    				{
    					if (mystring[j+1]<=90 && mystring[j+1]>=65);
    					{
    						word_cap++;
    					}
    				}
    
            if(mystring[j] == 0)
            {
                break;      
            }
            }
    		*word_count=num_of_words;
    		*cap_words=word_cap;
    }
    
    int main(void)
    {
            
            char mystring[100];
            int k, num_of_words, cap_words, histogram;
    
    		printf ("Enter a string: \n");
    
    		// Obtains string from user. 
            for (k=0; k<100; k++)
            {
                    scanf("%c",&mystring[k]);
                    if (mystring[k] == 10)
                    {
                            mystring[k] = 0;
                            break;
                    }
             }
    
    
            //printf("%s\n", mystring);
    
            sub (mystring, &num_of_words, &cap_words, &histogram);
            printf("The total number of words in the string = %d.\n", num_of_words);
    		printf("The number of words starting with a capital letter = %d. \n", cap_words);
           
           
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't like this part, before the main for loop. The logic you need, should go inside the loop, not scatter around, imo.

    Code:
    		// Calulates star of words with capital letters. 
    		if ( mystring[0]<=90 && mystring[0]>=65)
    				{
    					word_cap=1;
    				}
    I'd delete that part.

    The logic you need, needs to work with any word - first or last. Use the inside or outside variables to work with your counting of the caps, and it should all come together.

    Set all your variables up and start your for loop:
    Code:
    if array[i] is not a blank, and inside=0, then 
      inside=1;
      if( array[i] is a capital letter, then
        capitalized words++
    
    Capitalized word counting only allowed when inside was 0, 
    and is now 1, and array[i] is not a blank.
    
      if array[i] is a blank or a newline then
        inside = 0;
    etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. word count program not working
    By vsovereign in forum C Programming
    Replies: 5
    Last Post: 06-04-2010, 02:11 PM
  2. Count Number of Letters in a Word
    By quiksand in forum C Programming
    Replies: 10
    Last Post: 05-14-2010, 11:44 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

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