C++ 如何大写单词的第一个字母
这是我的输入txt文件C++ 如何大写单词的第一个字母,c++,c++11,C++,C++11,这是我的输入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));
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);
}
到
- 每个单词的首字母大写
- 在
之后插入新行
bool shouldCapitalize=true;
而(!inputFile.eof())
{
如果(CH=‘A’& CH<P>更多的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;
}
#包括
#包括
#包括
类字上限{
公众:
WordUpper():m_wasLetter(false){}
char运算符()(char c);
私人:
布尔穆瓦斯莱特;
};
char WordUpper::operator()(char c)
{
if(isalpha(c)){
如果(!m_wasLetter)c=toupper(c);
m_wasLetter=真;
}否则
m_wasLetter=假;
返回c;
}
int main()
{
std::ifstream in(“foo.txt”);
std::ofstreamout(“out.txt”);
std::transform(std::istreambuf_迭代器(in),std::istreambuf_迭代器(),
std::ostreambuf_迭代器(out),
WordUpper());
返回0;
}
您所做的编码有什么问题?它将每个单词大写,而不仅仅是单词的第一个字母,第二个字母在输出文件的fullstop之后没有向发出新行
#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;
}