码迷,mamicode.com
首页 > 其他好文 > 详细

编程 统计字符串最后一个单词【水】

时间:2019-10-09 19:09:38      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:substr   ret   std   end   content   思路   bst   return   getline   

题目描述

计算字符串最后一个单词的长度,单词以空格隔开。

输入描述:

一行字符串,非空,长度小于5000。

输出描述:

整数N,最后一个单词的长度。

示例1

输入

复制
hello world

输出

复制
5

思路:
rfind()
源码:

#include <iostream>
#include <string>
using namespace std;

int main()
{
  string str;
  string ResStr;
  int pos = 0;
  getline(cin, str);
  pos = str.rfind(" ");
  ResStr = str.substr(pos + 1);
  cout << ResStr.size() << endl;
  return 0;
}

 

我印象中string长度才几百,可能题目没有给过长的字符串吧,否则该算法不ac

编程 统计字符串最后一个单词【水】

标签:substr   ret   std   end   content   思路   bst   return   getline   

原文地址:https://www.cnblogs.com/liuruoqian/p/11643638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!