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

求教高手(一个输出首字母的程序),该如何处理

2012-05-27 
求教高手(一个输出首字母的程序)#includeiostream#includestring#include vectorusing namespace st

求教高手(一个输出首字母的程序)
#include<iostream>
#include<string>
#include <vector> 
using namespace std;
int main()
{
string line;
while(getline(cin,line))
{ vector<string> word;
int i=0;
int j=0;
for(;i<(line.size());i++)
{
if(line.substr(i,1)==" "||line.substr(i,1)=="-")
{
word.push_back(line.substr(j,i-j));j=i;
}
}
word.push_back(line.substr(j,i-j+1));
for(vector<string>::iterator p=word.begin();p!=word.end();p++)
{
if(*p!="a"&&*p!="an"&&*p!="the"&&*p!="for"&&*p!="and"&&*p!="of"&&*p!="A"&&*p!="AN"&&*p!="THE"&&*p!="FOR"&&*p!="OF"&&*p!="AND")
{cout<<(*p).substr(0,1);}
if(p==word.end()-1)
cout<<"\n";
}
}
return 0;
}
单词能被分割开,却不能通过{cout<<(*p).substr(0,1);}输出首字母,费解!
求教。
下为题目描述:
Description 

给定一个由若干英文单词组成的字符串,生成一个由各首字母组成的缩写词(acronym),其中的the,a,an,of,for及and被忽略。 

Input 

输入数据有若干行,每行上有一个字符串。字符串中分隔单词的字符为一个空格或一个连字符(’-’)。单词中不包含连字符。字符串的长度不超过100个字符。 

Output 

对于每一个字符串,输出结果,每行最后有一个换行符。 

Sample Input 

United States of America
PEOPLE’S REPUBLIC OF CHINA
The Asia-Pacific Economic Cooperation
Organization of the Petroleum Exporting Countries
United Nation Educational Scientific and Culture Organization
Universal Serial Bus 

Sample Output 

USA
PRC
APEC
OPEC
UNESCO
USB 
出自(ecnu online jidge)

[解决办法]

C/C++ code
...        for(; i < (line.size()); i++)        {            if(line.substr(i, 1) == " " || line.substr(i, 1) == "-")            {                word.push_back(line.substr(j, i - j));                j = i + 1;// 这里还要加一个' '或者'-',所以是i + 1            }        }        word.push_back(line.substr(j, i - j));// 上面已经加了,这里就不加1了...
[解决办法]
探讨
C/C++ code


...
for(; i < (line.size()); i++)
{
if(line.substr(i, 1) == " " || line.substr(i, 1) == "-")
{
word.push_back(line.substr(j……

[解决办法]
C/C++ code
#include<string>#include<cstring>#include<iostream>using std::string;const char* gStopWords[] = { "of ", "Of ", "OF ", "a ", "A ", "an ", "An ", "AN ", "the ", "The ", "THE ", "and ",        "And ", "AND ", "for ", "For ", "FOR " };const int gWordCount = sizeof(gStopWords) / sizeof(const char*);string capitals(string str) {    for (int i = 0; i < gWordCount; ++i) {        size_t index = str.find(gStopWords[i], 0);        while (index != string::npos) {            str.replace(index, strlen(gStopWords[i]), "");            index = str.find(gStopWords[i], index);        }    }    string ret("");    bool flag = true;    for (string::iterator iter = str.begin(); iter != str.end(); ++iter) {        if (flag) {            ret.append(1, *iter);            flag = false;        } else {            if (*iter == ' ' || *iter == '-') {                flag = true;            }        }    }    return ret;}int main() {    string str("The Asia-Pacific Economic Cooperation");    std::cout << capitals(str) << std::endl;    return 0;} 


[解决办法]

探讨

C/C++ code

...
for(; i < (line.size()); i++)
{
if(line.substr(i, 1) == " " || line.substr(i, 1) == "-")
{
word.push_back(line.substr(j, i - j……

热点排行