From f0726bd1b2110afd29964fbb6df1ae070b16070b Mon Sep 17 00:00:00 2001 From: Nikhil Dahake <136945761+NocStaRex@users.noreply.github.com> Date: Mon, 20 Oct 2025 01:13:51 +0530 Subject: [PATCH] Create length_of_last_word.cpp --- length_of_last_word.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 length_of_last_word.cpp diff --git a/length_of_last_word.cpp b/length_of_last_word.cpp new file mode 100644 index 00000000..7cb6c03e --- /dev/null +++ b/length_of_last_word.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int lengthOfLastWord(string s) { + int i = s.size() - 1; + while (i >= 0 && s[i] == ' ') --i; + int len = 0; + while (i >= 0 && s[i] != ' ') { + ++len; + --i; + } + return len; + } +};