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