English 中文(简体)
利用增强图书馆,在判刑时使用第一封信
原标题:capitalize first letter of each word in sentence using boost library

I was looking for a way to capitalize first letter of each word in a sentence using boost. To make the code consistent i.e the final output will not have any space or tab if present in the input sentence. To get individual words I m using boost::alogrithm::split and for combining them back boost::algorithm::join . but how do I get each first letter capitalized ?

我尝试了这部法律。

 #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("	 "), 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;                                                                   
 }    
最佳回答

这部法典是我工作的。

#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("	 "), 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; 
}
问题回答

The problem is to define how you determine what a sentence is. The simplest solution is that it is any sequence which ends with the regular expression "[.!?][" ]*" (since you ve already eliminated white space); this is actually simple enough that you could do it without regular expressions. Then memorize that you ve seen it, and capitalize the next word:

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 ==  ? ;
}

我赞赏这只字不提,与Unicode合作,但利用标准图书馆职能提供基本解决办法。 页: 1 也许不是最佳办法,而是选择:

#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;
}

这里,如果有人有兴趣的话,我的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== 	 )
    {
        return toupper(a);
    }
    return a;
});




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签