This is a simple program but i got a small problem with it.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char str[50];
int i=0,count=0;
char space;
printf("Enter a string : ");
// gets(str);
strcpy(str,"d ak");
printf("%s",str);
while(str[i]!='\0')
{
if(str[i]== ' ')
count++;
i++;
}
printf("\n\nThe total number of words are %d ",count+1);
printf("\n\n");
system("pause");
return 0;
}
Look at this line: strcpy(str,"d[space]ak");
Output: 2 words
But if i change to:
strcpy(str,"d[space]ak[space][space]d");
Output: 5 words
reason: When i increase one space, it will also increase 1 word count.
example: d[space]d = 2 words
But if: d[space][space]d = 3words
How can i solve this?