简答题
C++
题号 #2657817
编写一程序,统计一篇英文文章中单词的个数与行数。
参考答案
暂无
答案解析
#include<iostream>
#include<fstream>
using namespace std;
bool isalph(char);
int main(){
ifstream ifile("E:\\lenovo\\english\\daily.doc");
char text[1000];
bool inword=false;
int rows=0,words=0;
int i;
while(!ifile.eof()){
ifile.getline(text,999);
rows++;
i=0;
while(text[i]!=0){
if(!isalph(text[i]))
inword=false;
else if(isalph(text[i]) && inword==false){
words++;
inword=true;
}
i++;
}
}
cout<<"rows= "<<rows<<endl;
cout<<"words= "<<words<<endl;
ifile.close ();
return 0;
}
bool isalph(char c){
return ((c>='A' && c<='Z') || (c>='a' && c<='z'));
}
#include<fstream>
using namespace std;
bool isalph(char);
int main(){
ifstream ifile("E:\\lenovo\\english\\daily.doc");
char text[1000];
bool inword=false;
int rows=0,words=0;
int i;
while(!ifile.eof()){
ifile.getline(text,999);
rows++;
i=0;
while(text[i]!=0){
if(!isalph(text[i]))
inword=false;
else if(isalph(text[i]) && inword==false){
words++;
inword=true;
}
i++;
}
}
cout<<"rows= "<<rows<<endl;
cout<<"words= "<<words<<endl;
ifile.close ();
return 0;
}
bool isalph(char c){
return ((c>='A' && c<='Z') || (c>='a' && c<='z'));
}