Need a fix to word counting

This is a discussion on Need a fix to word counting within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #define STRINGSIZE 100 /*Prototypes*/ void getString(char *string); void lowToUpper(char lowerData[]); //convert the string ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    29

    Need a fix to word counting

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define STRINGSIZE 100
    
    /*Prototypes*/
    
    void getString(char *string);
    void lowToUpper(char lowerData[]); //convert the string to uppercase
    void upToLower(char upperData[]); //convert the string to lowercase
    int numGraph(char *string); //counts # of characters
    int CountWords(char *string); //counts # of words
    
    /*Main program input*/
    int main(int argc, char *argv[])
    {
      
      char string[STRINGSIZE];
      int lowerData, upperData;
      int count=0;
      int num=0;
    
       getString(string);
       printf("Entered string = %s", string);
    
       count = numGraph(string);
       printf("Number of characters in string = %d\n", count);
       
       num = CountWords(string);
      
      system("PAUSE");	
      return 0;
    }
    
    
    /*void codes*/
    
    void lowToUpper(char lowerData[])
         {
    
         }
         
    void upToLower(char upperData[])
         {
    
         }
    int numGraph(char *string)
    {
    	int i, count = 0;
    	for (i = 0; string[i] != '\0';i++)
    	{
    		if (isgraph((int)string[i])) count++; 
    	}
    	return count;
    }
    
    int CountWords(char *string)
    {
        char c;
        int num = 0;
        int flag = 0;
        while((c=getchar())!='\n')
        {
                                  if((c==' ')||(c==' ')||(c=='.')||(c==';')||(c==',')||(c==';')
                                  ||(c==':')||(c=='"')||(c=='?')||(c=='!')||(c=='-'))
        {
                  flag=0 ;
                  }
                  else if(flag==0)
                  {
                       num++;
                       flag=1;
                  }
        }
    
    printf("Number of words in string = %d\n",num);
    }
    
    void getString(char *string)
    {
       printf("Enter A String: ");
       fgets(string, STRINGSIZE, stdin);
    }
    whats wrong with my int CountWords(char *string)? The program has no error when complied, but it does not display word count
    Last edited by Charak; 02-24-2011 at 02:35 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't know, what's wrong with it?

    You tell me. What's it doing that it's not supposed to? People generally don't just like to read through pages of code with no idea what the program is doing. Sure, some people like compiling random code just to see what it does, and there are even some people that have nothing better to do than to read every line of code every random person on the internet asks them to read, but most people have better things to do. You should learn how to ask questions the smart way.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    Quote Originally Posted by quzah View Post
    I don't know, what's wrong with it?
    >>The program has no error when complied, but it does not display word count


    essentially, it should read how many words are in the string and tell you, but its not coming up when runned

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you entering a string when prompted?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    yes, but no return on word count

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
        while((c=getchar())!='\n')
    Are you typing in words there? Because you are asking for input from the keyboard.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    got it to work

    Code:
    int countWords(char *string)
    {
        int i, count = 0;
        for ( i = 0; string[i]; ++i ) 
        {
            if (isspace(string[i])) ++count;
        }
        return count;
    }

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    So "hello world" gives you three words? right?
    Is it what you want? or your definition of word?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. word counting program
    By mashour06 in forum C Programming
    Replies: 2
    Last Post: 06-09-2009, 03:58 AM
  2. Counting the characters from each word from a text file
    By flipguy_ph in forum C Programming
    Replies: 6
    Last Post: 04-27-2009, 05:56 PM
  3. Replies: 2
    Last Post: 12-02-2007, 04:40 AM
  4. Word Counting Help(newbie)
    By MedicKth in forum C++ Programming
    Replies: 6
    Last Post: 09-19-2003, 12:30 PM
  5. Word Counting
    By Achillles in forum C++ Programming
    Replies: 9
    Last Post: 09-11-2002, 02:09 PM

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