C program for counting the number of characters and words in a given string

C program for counting the number of characters and words in a given string 


Program to find number Chars in given String, without strlen function, counting chars in given line


#include
#include
int main()
{
 int count_words=0,i;
 int count_char=0;
 char str[20];
 printf("Enter string : ");
 gets(str);
 for(i=0; str[i]!=NULL; i++)
 {
   count_char++;
   if(str[i]==' ')
      count_words++;
 }
 printf("\nNumber of characters in string : %d",count_char);
 printf("\nNumber of words in string : % d",count_words+1);
 getch();
 return 0;
}

No comments:

Post a Comment