这个是通过对文本文件中的读取来录入字符串,从而实现统计文本文件中单词或词语的个数
代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
using namespace std;
int match(char str[])
{
int count = 0;
int state = 1;
int i = 0;
while (str[i] != '\0')
{
if (str[i] == ' ')
{
state = 1;
}
else if (state == 1)
{
state = 0;
count++;
}
i++;
}
return count;
}
int main()
{
cout << "从D盘中读取Text.txt:" << endl;
ifstream file("D:\\Text.txt");
if (file.fail()){ cout << "文件不存在!" << endl; }
char T[255];
file.get(T,255);
cout << "输入的字符串为:" << T << endl;
int length = 0;
length = match(T);
cout << "字符串中单词