【牛客】字符串最后一个单词的长度

代码:

#include
using namespace std;

int main()
{
    string str;
    getline(cin, str);
    int count = 0;
    
    for(int i = str.size() - 1; i >= 0; i--)
    {
        if(str[i] == ' ')
            break;
        
        count++;
        
    }

    cout << count << endl;
    return 0;
}

 

你可能感兴趣的:(牛客)