Docstoc

C++ Function to count the number of words

Document Sample
C++ Function to count the number of words Powered By Docstoc
					                                       17
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include <ctype.h>
// Function to count the number of words.
void calculate()
{
        fstream tfile;
        clrscr();
        tfile.open("PARA.TXT", ios::in);
        char arr[80];
        char ch;
        int i=0, sum=0, n=0;
        while(tfile)
        {
                tfile.get(ch);
                arr[i] = ch;
                i++;
                if (ch == ' ')
                {
                        i--;
                        sum = sum + i;
                        i = 0;
                        n++;
                }
        }
        cout << "Total no. of words : " << n;
}
void main()
{
        clrscr();
        calculate();
}
step by step explanation:
1.the header file "iostream.h" is used to include the function cin and cout.
2.the header file"conio.h"is used to include the function like getch and clrscr.
3.void calculate is also a void type function.
4.while is a conditional statement it is used to check the condition.
5.void main is also a void type function it is worked under the curley brackets.




                                     Page 1

				
DOCUMENT INFO
Shared By:
Stats:
views:48
posted:3/15/2012
language:English
pages:1
Description: C programs on various topics such as count and display number of vowels used in a text file, function to append records, function to calculate average word size, C function along with program to merge two files using file handling concept of C programming language.