what would I use to count number of words
<< split from long dead thread http://cboard.cprogramming.com/c-pro...ber-words.html >>
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
clrscr();
char *str,a[]=".!?";
int index,cnt,sntcnt=0,slen=0;
printf("Enter string: ");
gets(str);
slen=strlen(str);
for (index=0;index<slen;index++)
{
for (cnt=0;cnt<4;cnt++)
{
if (str[index]==a[cnt]||str[index]=='\0')
{
sntcnt;
}
}
}
printf("Number of sentence in the string: %d",sntcnt);
getch();
return 0;
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
clrscr();
char *str;
int index,slen=0,wrdcnt=0;
printf("Enter string: ");
gets(str);
slen=strlen(str);
for (index=0;index<=slen;index++)
{
if (str[index]==' '||str[index]==','||str[index]=='.'||str[index]=='!'||str[index]=='?'||str[index]=='\0')
{
wrdcnt++;
if (str[index]==' ')
{
wrdcnt--;
}
else
{
wrdcnt++;
}
}
}
printf("The number of words in the string is: %d",wrdcnt);
getch();
return 0;
}
help with my codes guys..im really new >.<
if i input more than 2 spaces between the words the program also counts em >.<
same with the sentence count program