首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

怎样指定文件中统计一个字符串出现的次数(C/C++),该怎么处理

2012-02-27 
怎样指定文件中统计一个字符串出现的次数(C/C++)求助各位大虾:怎样指定文件中查找一个字符串并显示该字符

怎样指定文件中统计一个字符串出现的次数(C/C++)
求助各位大虾:怎样指定文件中查找一个字符串并显示该字符串出现的次数,比如说在D:\tools\abc.txt文件中寻找bbc字符串出现的次数   。最好帮忙写个完整的程序,谢谢


[解决办法]
统计文件的单词数
读文件的一行


int main()
{
ifstream infile;
string filename;
cout < < "Please enter the file name: ";
cin > > filename;

infile.open(filename.c_str());
string line;
getline(infile, line, '\n ');
infile.close();

vector <string> wordsOfLine;
string::size_type pos = 0, prev_pos =0;
string word;
while ((pos = line.find_first_of( ' ', pos)) != string::npos)
{
word = line.substr(prev_pos, pos - prev_pos);
prev_pos = ++pos;
wordsOfLine.push_back(word);
}
wordsOfLine.push_back(line.substr(prev_pos, pos - prev_pos));

size_t numOfLine = wordsOfLine.size();
cout < < numOfLine < < "words " < < endl;
}
读整个文件的:

int main()
{
ifstream infile;
string filename;
cout < < "Please enter the file name: ";
cin > > filename;

infile.open(filename.c_str());
string line;
vector <string> wordsOfFile;
while (getline(infile, line, '\n '))
{
string::size_type pos = 0, prev_pos =0;
string word;
while ((pos = line.find_first_of( ' ', pos)) != string::npos)
{
word = line.substr(prev_pos, pos - prev_pos);
prev_pos = ++pos;
wordsOfFile.push_back(word);
}
wordsOfFile.push_back(line.substr(prev_pos, pos - prev_pos));
}

infile.close();

size_t numOfLine = wordsOfFile.size();
cout < < numOfLine < < "words " < < endl;

return 0;
}

加上判断即可,看着用吧,呵呵^_^okokok
[解决办法]
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int CountSubString(string const& str, string const& substr)
{
int nCount = 0;

string::size_type substrSize = substr.size();
string::size_type idxSub = str.find(substr, 0);
while (idxSub!=string::npos) {
++nCount;
++idxSub;
idxSub = str.find(substr, idxSub);
}

return nCount;
}

int CountStrInFile(string const& filename, string const& str)
{
ifstream inf(filename.c_str());
if (!inf) {
cout < < "Error: can 't open the file: " < <filename < <endl;
exit(1);
}

string infStr;
int nSubStrFound = 0;
while (inf && !inf.eof()){
inf> > infStr;
nSubStrFound += CountSubString(infStr, str);
}

return nSubStrFound;
}


int main()
{
string filename( "d:\\temp\\test.txt "); // the file name to search string
string strToCount( "abc "); // the string to count

int nCount = CountStrInFile(filename, strToCount);
cout < <nCount < < " times of \ " " < <strToCount < < "\ " found in file: " < <filename < <endl;

strToCount = "aaa ";
nCount = CountStrInFile(filename, strToCount);
cout < <nCount < < " times of \ " " < <strToCount < < "\ " found in file: " < <filename < <endl;



system( "pause ");
return 0;
}

//测试文件test.txt内容:
abc abc abc lpte yejylyyryryljryjrabc
logyy[yuuujabcabc
aaaaaa aabeee aaa

//测试输出:
6 times of "abc " found in file: d:\temp\test.txt
5 times of "aaa " found in file: d:\temp\test.txt

[解决办法]
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
char str1[255],str2[255],*p1,*p2, *temp;
int sum=0;
cout < < "intput two strings " < <endl;
cin> > str1;
cin> > str2;
p1=str1;
p2=str2;

while (*p1!= '\0 ')
{
temp = p1;
if(*temp==*p2)
{

while((*temp==*p2)&&(*p2!= '\0 ')&&(*temp!= '\0 '))
{
temp++;
p2++;
}
}
p1++;
if(*p2== '\0 ') sum=sum+1;
p2=str2;
}
cout < <sum;
return 0;

}

热点排行
Bad Request.