Leetcode 58 Length of Last Word 难度:0

时间:2024-10-21 23:03:44

https://leetcode.com/problems/length-of-last-word/

int lengthOfLastWord(char* s) {
int ans = 0;
int fans = 0;
for(int i = 0;s[i];i++){
if (s[i] ==' '){fans = ans;ans = 0;while(s[i + 1] == ' '){i++;}}
else ans++;
}
return ans?ans:fans;
}