这是我输入的txt文件
i like apple and i love to eat apple.are you like to eat apple.
我想将此文件输出到另一个文本文件中,其中必须在完全停止后插入新行,并且每个单词必须大写,就像我们在php或python中使用Toupper一样。我该怎么做?
这是我所做的编码:
inputFile.get(ch);
while (!inputFile.eof())
{
outputFile.put(toupper(ch));
inputFile.get(ch);
}
答案 0 :(得分:2)
更多C ++方式:
#include <fstream>
#include <iterator>
#include <algorithm>
class WordUpper {
public:
WordUpper() : m_wasLetter( false ) {}
char operator()( char c );
private:
bool m_wasLetter;
};
char WordUpper::operator()( char c )
{
if( isalpha( c ) ) {
if( !m_wasLetter ) c = toupper( c );
m_wasLetter = true;
} else
m_wasLetter = false;
return c;
}
int main()
{
std::ifstream in( "foo.txt" );
std::ofstream out( "out.txt" );
std::transform( std::istreambuf_iterator<char>( in ), std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>( out ),
WordUpper() );
return 0;
}
答案 1 :(得分:1)
要
.
做的:
bool shouldCapitalize = true;
while (!inputFile.eof())
{
if (ch >= 'a' && ch <= 'z')
{
if (shouldCapitalize)
outputFile.put(toupper(ch));
else
outputFile.put(ch);
shouldCapitalize = false;
}
else
{
if (ch == ' ') // before start of word
shouldCapitalize = true;
outputFile.put(ch);
}
if (ch == '.')
{
shouldCapitalize = true;
outputFile.put('\n');
}
inputFile.get(ch);
}