C ++中最后一个字的长度
因此,如果输入就像“我喜欢编程”,那么输出将为11
为了解决这个问题,我们将遵循以下步骤-
n:=0
对于字符串中的每个单词temp-
n:=温度大小
返回n
示例
让我们看下面的实现以更好地理解-
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int lengthOfLastWord(string s){
stringstream str(s);
string temp;
int n = 0;
while (str >> temp)
n = temp.size();
return n;
}
};
main(){
Solution ob;
cout << (ob.lengthOfLastWord("I love Programming"));
}输入值
"I love Programming"
输出结果
11