Title Description
Calculating the length of the last word of the string, the words separated by a space.
Enter a description:
His string, a non-empty, a length of less than 5000.
Output Description:
Integer N, the length of the last word.
Example 1
Export
copy5
problem-solving ideas: I think the direct use c ++ string type find_last_of () member function, find a space where the last number re-use string length function to do the appropriate operation on the ok
Note: When a string with no spaces It is a special case
#include <iostream> #include <cmath> #include <algorithm> #include <set> #include <string> #include <string.h> #include <cstdio> /*@author:浅滩 *family: *time: * / // I'm like a child playing at the beach, // time to time to pick up more than the usual smooth stones or more beautiful shells rejoice, // and demonstrated in front of me is totally unproven truths sea a using namespace std; int main () { string str; int i; getline(cin,str); i=str.find_last_of(' '); if(i==-1) cout<<str.size()<<endl; //cout<<str.size()<<endl; else cout<<str.size()-(i+1); //cout << "Hello world!" << endl; return 0; }
Reproduced in: https: //www.cnblogs.com/cstdio1/p/11018097.html