Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include <ctype.h>
int main()
{
char str[50];
int words = 0, len=0, i;
int spaces=0;
printf("\n--------------------------------------------------------------------------");
printf("\n\nPROGRAM TO COUNT THE NUMBER OF WORDS, STRING, SPACE IN A GIVEN STRING");
printf("\n\n--------------------------------------------------------------------------");
printf("\n\n\t ENTER A STRING...: ");
//gets(str);
strcpy( str, "a b c d e");
while(str[len]!='\0') len++;
for(i=0;i<=len;i++)
{
while( isspace( str[i] )) { spaces++; i++; }
{continue;}
}
for(i=0;i<len;i++)
{
while( !isspace( str[i] )) { i++; }
words++;
}
printf("\n\t NUMBER OF WORDS IN THE ABOVE SENTENCE IS...: %u", words);
printf("\n\n-------------------------------------------------------");
printf("\n\t NUMBER OF STRING IN THE ABOVE SENTENCE IS...: %u", len);
printf("\n\n-------------------------------------------------------");
printf("\n\t NUMBER OF STRING IN THE ABOVE SENTENCE IS...: %u", spaces);
printf("\n\n-------------------------------------------------------");
getch();
return 0;
}
Hi, everything work fine if i input some number or char.
example: strcpy( str, "a b c d e");
output: 5 word 9 string 4 space
But if i input: strcpy( str, "[space][space][space]");
Output: 3 word 3 string 3 space
This is the error. I should be getting 0 word 3 string 3 space.
Can someone helping me out?