Complete Program.
int word_count(ifstream &in ); // return the number of words in text 1
int character_count(ifstream &in); // return the number of characters
#include "stdafx.h"
#include <iostream>
# include <string>
# include<fstream>
using namespace std;
int main()
{ ifstream in; in.open(“text1.txt”);
cout<< “number of words”<< word_count(in )<<endl;
cout<< “number of characters”<<character_count(in)<<endl;
in.close( );
}
//int word_count(ifstream &in ); // return the number of words in text 1
//int character_count(ifstream &in); // return the number of characters
// #include "stdafx.h"
#include <iostream>
# include <string>
# include<fstream>
using namespace std;
int word_count(ifstream &in ){
int word=1;
char ch;
in.seekg(0,ios::beg);
while(in)
{
in.get(ch);
if(ch==' '||ch=='\n')
word++;
}
return word;
}
int character_count(ifstream &in){
char ch;
int i, c=0, sp=0;
while(in)
{
in.get(ch);
i=ch;
if((i > 63 && i < 91) || (i > 96 && i < 123))
c++;
else
if(ch== ' ')
sp++;
}
return sp;
}
int main()
{
ifstream in; in.open("text1.txt");
cout<< "number of words: "<< word_count(in )<<endl;
cout<< "number of characters: "<<character_count(in)<<endl;
in.close( );
}
Comments
Leave a comment