58-length-of-last-word

DevGod
Vtuber
int lengthOfLastWord(char* s) { int leng = strlen(s)-1; int I = leng; int score = 0;
while(I>=0){ if(s[I] != ' '){ score += 1; }else{ if(score > 0){ return score; } } I+=-1; }
return score;}
class Solution { public int lengthOfLastWord(final String mySTR) { //Reverse String StringBuilder sb = new StringBuilder(); int I=mySTR.length()-1; while(I>=0){ sb.append(mySTR.charAt(I)); I+=-1; } String newSTR = sb.toString();
//Count Last Word int counter = 0; boolean letter = false; for(char a: newSTR.toCharArray()){ if(a != ' '){letter=true; counter++;} else if(letter){break;} }
return counter; }}
/** * @param {string} s * @return {number} */var lengthOfLastWord = function(s) { return s.trim().split(" ")[s.trim().split(" ").length-1].length;};