c++输入输出流求解
在words.txt文件中包含了87314个单词,编写C++程序从words文件中读取单词,并输出重复字母对最多的单词,将第一个最多重复字母对的单词写入newwords.txt文件中。例如tooth这个单词有一个重复字母对,committee有三个重复字母对。要求写注释。
2021-11-22 15:20
2021-11-23 07:59
程序代码:#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int foo( const std::string& word )
{
int count = 0;
for( const char* p=word.c_str(); *p; ++p )
count += *p == *(p+1);
return count;
}
int main( void )
{
int count_max = -1;
string word_max;
ifstream fin( "words.txt" );
for( string word; fin>>word; )
{
int count = foo(word);
if( count_max < count )
{
count_max = count;
word_max = word;
}
}
ofstream fout( "newwords.txt" );
fout << word_max;
}
2021-11-23 08:16