1

我正在寻找一种使用 boost 将句子中每个单词的首字母大写的方法。为了使代码一致,即如果输入语句中存在,最终输出将不包含任何空格或制表符。为了获得单个单词,我使用 boost::algorithm::split 并将它们组合回 boost::algorithm::join 。但是如何让每个首字母大写?

我试过这段代码

 #include <iostream>                                                              
 #include <vector>                                                                
 #include <boost/algorithm/string/split.hpp>                                      
 #include <boost/algorithm/string.hpp>

 int main()                                                                      
 {                                                                               
     using namespace std;                                                        

     string str("cONtainS            SoMe    CApiTaL WORDS");                    

     vector<string> strVec;                                                      
     using boost::is_any_of;                                                     
     using boost::algorithm::token_compress_on;                                  

     boost::algorithm::split(strVec, str, is_any_of("\t "), token_compress_on);  

     vector<string>::iterator i ;                                                

     for(i = strVec.begin() ; i != strVec.end(); i++)                            
     {
         (*i)[0] = boost::to_upper((*i)[0]);
         cout<<*i<<endl;                                                             
     }                                                                           

     return 0;                                                                   
 }    
4

4 回答 4

2

问题是定义你如何确定一个句子是什么。最简单的解决方案是它是以正则表达式结尾的任何序列"[.!?][\"\']*"(因为您已经消除了空格);这实际上很简单,你可以在没有正则表达式的情况下做到这一点。然后记住你已经看过它,并将下一个单词大写:

bool atEndOfSentence = true;
for ( std::vector<std::string>::const_iterator current = words.begin();
        current != words.end();
        ++ current ) {
    if ( atEndOfSentence ) {
        (*current)[0] == toupper( (*current)[0] );
    }
    std::cout << *current << std::endl;
    atEndOfSentence = isSentenceEnd( 
            *std::find_if( current->rbegin(), current->rend(),
                           IsNotQuoteChar() ).base() );
}

和:

struct IsNotQuoteChar
{
    bool operator()( char ch ) const
    {
        return ch != '\'' and ch != '\"';
    }
};

和:

bool
isSentenceEnd( char ch )
{
    return ch == '.' || ch == '!' || ch == '?';
}
于 2012-05-11T13:00:22.947 回答
1

我很欣赏这不使用 Boost 并且不适用于 Unicode,但提供了使用标准库函数的基本解决方案。我正在打破isalpha文字的界限。也许不是最好的方法,但这只是一种选择:

#include <string>
#include <iostream>

using namespace std;

int main()
{
    string str("  cONtainS            SoMe    CApiTaL WORDS");

    bool niw(true);
    string strC;
    for (size_t i = 0; i < str.size(); ++i)
    {
        if ( niw && isalpha( str[i] ) )
        {
            strC += toupper( str[i] );
            niw = false;
        }
        else if ( ! niw )
        {
            if  ( isalpha( str[i] ) )
                strC += tolower( str[i] );
            else
            {
                niw = true;
                strC += str[i];
            }
        }
        else
            strC += str[i];
    }

    cout << str << endl;
    cout << strC << endl;
}
于 2012-05-11T13:06:23.830 回答
0

这段代码让我工作

#include <iostream>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <vector>
#include <ctype.h>

int main()
{
    using namespace std;

    string str("contAins Some       CapItal WORDS");
    string result;

    vector<string> strVec;

    using boost::is_any_of;
    using boost::algorithm::token_compress_on;

    boost::algorithm::split(strVec, str, is_any_of("\t "), token_compress_on);

    vector<string>::iterator i; 

    for(i = strVec.begin(); i !=strVec.end(); ++i)
    {                                                                                

        boost::to_lower(*i);
        (*i)[0]=toupper((*i)[0]);

        cout<<(*i)<<endl;
        result += *i +" ";
    }  

    boost::trim_right(result);
    cout<<result;
    return 0; 
}
于 2012-05-12T07:37:27.450 回答
0

如果有人感兴趣,这是我的 C++11 解决方案:

std::string s("some lowercase string");
s[0] = toupper(s[0]);
std::transform(s.begin()+1, s.end(),s.begin(),s.begin()+1, 
[](const char& a, const char& b) -> char
{
    if(b==' ' || b=='\t')
    {
        return toupper(a);
    }
    return a;
});
于 2016-03-14T23:19:04.383 回答