9.16.2011

Count number of wrods and characters in string

/* C programe for counting the number of characters and words in a given string */
#include<stdio.h>
#include<conio.h>
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;
}

     Output of above program :

Enter string : c programming codes
Number of characters in string : 19
Number of words in string : 3


Output of How to count Numbers of  Words and Characters in String C program
Figure: Screen shot for How to count Numbers of
Words and Characters in String C program

7 comments:

  1. please chck in this pgm . it wont work with proper output hopefully ...

    ReplyDelete
  2. the header file is missing
    c is reed as both char and word which is wrong

    ReplyDelete
  3. can anyone please tell me what is the purpose of the second line in the program?

    ReplyDelete
    Replies
    1. #rashi

      In above C program line number is not mention. so write down the whole line.

      Delete
    2. Conio.h is used for including getch() type of functions in our program

      Delete
  4. To count the number of characters in string passed as argument in form of character
    array without using library function in c++

    ReplyDelete