C++ 如何在用陣列一個FILE中找特定的PHRASE?

2017-05-15 10:57 pm

回答 (1)

2017-05-16 7:18 am
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main(void){
ifstream fin;
string phrase;
unsigned len, cnt, ind;
cin >> phrase;
len = phrase.size();
char *a = new char[len+1];
fin.open("in.txt");
a[len]='\0';
for(ind=0; !fin.eof() && ind < len-1; ++ind) a[ind] = fin.get();
for(cnt=0; !fin.eof(); ind%=len){
a[ind++] = fin.get();
if(phrase == (string(a).substr(ind,len-ind) + string(a).substr(0,ind))) ++cnt;
}
cout << "file in.txt has " << cnt << " instances of " << phrase << endl;
delete a; a=NULL;
fin.close();
return 0;
}


收錄日期: 2021-04-30 23:08:54
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20170515145738AAwdngE

檢視 Wayback Machine 備份