如何在字符串C ++中找到最短的单词

时间:2016-04-30 18:04:08

标签: c++ shortest

我需要帮助。 我有一个功能,在句子中打印最长的单词。 但是如何显示最短的单词?

string text ="我的名字是Bob&#34 ;;

template <class T, class>
void fn(T t) { }

template <class T, class>
void fn(T t) { }

4 个答案:

答案 0 :(得分:1)

评论部分中提出的建议可行,只需重新安排控制结构即可实现。即

for(int i=0; i < text.length(); i++)
{
    /// If founded space, rewrite word
    if(text[i] != ' ')
        tmpWord += text[i];
    else
    {
       if(minWord.length()==0)//this only happens once
              minWord=tmpWord;//for the first word,you need to assign minWord so you have something to compare to

       if(tmpWord.length() < minWord.length() )//move this block here
            minWord=tmpWord;

       tmpWord = "";
    }

}

我可以补充一点,如果您使用istringstream提取operator>>,则可以轻松检查单词。类似的东西:

    #include <sstream>
    ....

    string text="my name is bob";
    string tmpWord = "";
    string minWord = "";
    istringstream ss(text);//defines the input string stream and sets text in the input stream buffer

    while(ss.peek()!=EOF)//until the end of the stream
    {
        ss>>tmpWord;//read a word up to a space

       if(minWord.length()==0)//this only happens once
              minWord=tmpWord;

       if(tmpWord.length() < minWord.length() )
            minWord=tmpWord;

    }

答案 1 :(得分:1)

  if (direction.isOK()) {
    // Do something

    //Toast.makeText(MapsActivity.this, "we here", Toast.LENGTH_LONG).show();
    Route route = direction.getRouteList().get(0);
    Leg leg = route.getLegList().get(0);
//steps here
    step = leg.getStepList();

    final ArrayList<LatLng> sectionList = leg.getDirectionPoint();

    CountDownTimer countDownTimer = new CountDownTimer(1000, 1000) {
    int i = 0;
    //go from first point to end point.
    @Override
    public void onTick(long millisUntilFinished) {

    if(i < step.size()) {

    start = new LatLng(step.get(i).getStartLocation().getLatitude(),step.get(i).getStartLocation().getLongitude());
    //set marker at new position
    marker.setPosition(start);
    i++;

    }

    }

    @Override
    public void onFinish() {

    }
    }.start();

答案 2 :(得分:0)

void ShortestWord(string text)
{
string tmpWord = "";
// The upper bound of answer is text
string minWord = text;

for(int i=0; i < (int)text.length(); i++)
{
    /// If founded space, rewrite word

    if(text[i] != ' ')
    {
        tmpWord += text[i];
    }
    else
    {
        // We got a new word, try to update answer
        if(tmpWord.length() < minWord.length())
            minWord=tmpWord;
        tmpWord = "";
    }

}
// Check the last word
if(tmpWord != "")
{
    if(tmpWord.length() < minWord.length())
        minWord=tmpWord;
}
cout << "Shortest Word: " << minWord << endl;
cout << "Word Length: " << minWord.length() << endl;
}

答案 3 :(得分:0)

如果我们想要获得最小值和最大值,则初始化值应与每个值相对。 实际上,这应该是&text;&#39;的最大限制字符串 在业务应用程序的开发中,这是常识,但有些程序员可能会讨厌这种方式。

string minWord = text; // MAX_SIZE
string maxWord = "";

for(int i = 0; i < text.length(); i++)
{
    /// If founded space, rewrite word
    if(text[i] != ' ')
        tmpWord += text[i];

    if(text[i] == ' ' || i == text.length()) {
        /// All the time check word length and if tmpWord > maxWord => Rewrite.
        if(tmpWord.length() > maxWord.length())
            maxWord = tmpWord;
        if(tmpWord.length() < minWord.length())
            minWord = tmpWord;

        tmpWord = "";
    }
}