一.问题描述
统计一篇英文文章中单词的个数与行数。
二.设计思路
三.流程图
四.伪代码
1
五.代码实现
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string word;
int main()
{
ofstream ofs;
ofs.open("D:\\VisualStudio2022\\Word.txt", ios::out);
if (!ofs)
{
cout << "文件打开失败" << endl;
return 0;
}
getline(cin, word);
ofs << word;
ofs.close();
ifstream ifs;
ifs.open("D:\\VisualStudio2022\\Word.txt", ios::in);
if (!ifs)
{
cout << "文件打开失败" << endl;
return 0;
}
int ans = 0;
char c;
while ((c = ifs.get())!=EOF)
{
if (c == ' ')
ans++;
}
ifs.close();
cout << "单词数量为:" << ans+1 << endl;
return 0;
}