字符串最后一个单词的长度

来源:互联网 发布:淘宝盗版软件举报 编辑:程序博客网 时间:2024/05/22 07:53

编程技巧:

auto可以自定义类型,适用于不知道数据类型的数据定义。

巧妙使用s.find(str);s.rfind(str);s.find_fisrt_of(str);s.find_last_of;s.find_first_not_of(str);s.find_last_not_of(str)等函数,其中后四个都是查找的str包含的任何一个字符。


#include <iostream>#include <string>using namespace std;//末尾可能存在空格版本int main(){    string str;        getline(cin,str);    auto pos=str.find_last_not_of(" ");//找到最后一个字母位置    str=str.substr(0,pos+1);//截断到最后一个字母    pos=str.rfind(" ");    str=str.substr(pos+1);    cout << str.size() << endl;}

1 0
原创粉丝点击