
January 18th, 2013, 10:07 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 3
Time spent in forums: 1 h 44 m 42 sec
Reputation Power: 0
|
|
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char str[50];
int words = 0, len = 0, i, number;
// clrscr();
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);
number = strlen(str);
while(str[len]!='\0')
len++;
// len--;
for(i=0;i<=len;i++)
{
if((str[i] == ' ' && str[i+1] != ' ') || i == len)
words++;
}
printf("\n\t NUMBER OF WORDS IN THE ABOVE SENTENCE IS...: %d", words);
printf("\n\n-------------------------------------------------------");
printf("\n\t NUMBER OF STRING IN THE ABOVE SENTENCE IS...: %u", number);
printf("\n\n-------------------------------------------------------");
getch();
return 0;
}
I'm able to count the number of string.  but I'm still confuse with the space counting? would you help me out on the code? 
|