如何将单词的第一个字母大写
How to Capitalize first letter of word
这是我的输入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);
}
更多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;
}
到
- 将每个单词的第一个字母大写
- 在
.
之后插入新行
do:
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);
}
相关文章:
- 如何将行的第一个单词作为一个变量,将行的其余部分作为另一个变量进行归档/存储
- 当字符串中只有一个"GOOD"字符串可用时,从没有空格的字符串中提取第一个单词的最佳和有效方法?
- QT 创建器只会将第一个单词从文本编辑转换为纯文本
- 从文件的一行中查找数字的平均值,避免使用第一个单词
- 删除 C++ 中字符串中第一个单词之前的空格
- C 如何在字符串中大写每个句子/行的第一个单词
- 大写每个句子中的第一个单词
- 检查字符串中的第一个单词是否等于 "print"
- 字符串将第一个单词移动到字符串的末尾
- C++文件输出只接受第一个单词
- 获取字符串的第一个单词并从实际字符串中删除该单词
- 字符串:使用 strcmp 查找第一个单词是否重复
- 我的算法只是将第一个单词写入文件
- C ++将文本文件中的读取到向量中,<vector>然后根据内部向量中的第一个单词写入向量或数组
- string.length()在第一个单词后停止
- C++ "cin"只读第一个单词
- 获取字符'='之前的所有标准::字符串或第一个单词
- 检测重复的单词 C++,不检测第一个单词
- 在控制台中输入字符串时.然后只打印第一个单词
- 如何从字符串中删除第一个单词