Wednesday 17 October 2012

Function032

Write a C function wordcount ( ) to count the number of words in a given string and then call in main ( ).

#include <stdio.h>
#include <conio.h>
int wordcount  (char     str[] )
{            
          int     i, w = 1;
          for ( i = 0; str[i] != '\0'; i++ )
          {                                      
                      if ( str[i] == ' ' )
                      {                             
                                  w++;
                      }
          }
          return w;
}                         
                        
void main ( )
{           
        char         str[40];
        int            w = 0;
        clrscr ( );
                           
        printf("\n\n\n\t\t Enter a string..:"); gets(str);
        w = wordcount ( str );
        printf("\n\n\t\t Number of words = %d",w);
        getch ( );
}
Click here to contact us

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...