fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int lengthOfLastWord(string s) {
  5. int len = s.length();
  6. int i = len - 1, j = 0;
  7. //ignore the right spaces
  8. while(s[i] == ' '){
  9. i--;
  10. }
  11. // go till a space is not found
  12. while(i >=0 && ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))){
  13. i--;
  14. j++;
  15. }
  16. return j;
  17. }
  18.  
  19. int main() {
  20. cout << lengthOfLastWord("hello world");
  21. cout << endl;
  22. return 0;
  23. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
5