-4

これは私の入力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);
}
4

2 に答える 2

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;
}
于 2013-02-23T20:02:07.910 に答える
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);
}
于 2013-02-23T19:37:35.730 に答える