From cc82354b98e607f59dfb13dcc13cf8fc68a58aed Mon Sep 17 00:00:00 2001 From: SaloniK17 <72257250+SaloniK17@users.noreply.github.com> Date: Mon, 12 Oct 2020 02:09:35 -0700 Subject: [PATCH] Add word count in string using pointer program is added --- Strings/word_count_in_string.c | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Strings/word_count_in_string.c diff --git a/Strings/word_count_in_string.c b/Strings/word_count_in_string.c new file mode 100644 index 0000000..f18cb0c --- /dev/null +++ b/Strings/word_count_in_string.c @@ -0,0 +1,42 @@ +problem name :count word in the string +//input formate string +//output formate int +##sample input +very good +##sample ouput +2 +------------------------ +#include +#include +int wordCount(char*p) +{ +char *q; +int wc=0; +q=p; +while(*q==' ')q++; +while(*q) +{ +if(*q==' ') +{ +wc++; +while(*q==' '&&*q)q++; +} +else q++; +} +if(q>p&&*(q-1)==' ')wc--; +if(q==p)wc--; +wc++; +return wc; +} +int main() +{ +char a[52]; +int x; +printf("enter the string:"); +fgets(a,52,stdin); +a[strlen(a)-1]='\0'; +fflush(stdin); +x=wordCount(a); +printf("total number of word in string is %d\n",x); +return 0; +} \ No newline at end of file