首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

出山题大家做做,C++的

2012-09-10 
出道题大家做做,C++的1.Reverse the words in a given English sentence (string) in C or C without requ

出道题大家做做,C++的

1.      Reverse the words in a given English sentence (string) in C or C++ without requiring a separate buffer to hold the reversed string (programming)

For example:

Input:  REALLY DOGSDISLIKE MONKEYS

Output: MONKEYS DISLIKEDOGS REALLY

 

我贴上我的答案,抛砖引玉,大家帮忙看看有什么不足:

// suppose use of temp variable is allowed// there is only one space between two wordsvoid reverseSentence(string &strSentence){    vector<string> words;    string::size_type offsite = 0, sepIndex = string::npos;     // extract every words into "words"    while ((sepIndex = strSentence.find(' ', offsite)) != string::npos)    {        words.push_back(strSentence.substr(offsite, sepIndex - offsite));        offsite = sepIndex + 1;    }    if (strSentence.size() > offsite)        words.push_back(strSentence.substr(offsite, strSentence.size() - offsite));        if (words.empty())        return;    // form a new sentence    strSentence.clear();    size_t size = words.size();    for (size_t u=size; u>0; u--)    {        if (u < size) strSentence.append(" ");        strSentence.append(words[u - 1]);    }}

1楼houhouzhe昨天 18:20
这道题太简单了,没有兴趣?

热点排行