leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 167ec37428ddc22e964c86d8e5926dba953b137b
parent b14d4d2f2c1ed06c6ff7310ab1e5703b6a4c17b7
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Tue, 11 Apr 2023 11:35:38 -0500

completed length of last word problem

Diffstat:
Alength-of-last-word/a.out | 0
Alength-of-last-word/length-of-last-word.cpp | 42++++++++++++++++++++++++++++++++++++++++++
2 files changed, 42 insertions(+), 0 deletions(-)

diff --git a/length-of-last-word/a.out b/length-of-last-word/a.out Binary files differ. diff --git a/length-of-last-word/length-of-last-word.cpp b/length-of-last-word/length-of-last-word.cpp @@ -0,0 +1,42 @@ +#include <iostream> +#include <string> +using namespace std; + + +// Speed Memory +//Total 0ms 6.5MB +//Beats 100% 92.69% +// + + + + +int lengthOfLastWord(string s) { + int len = 0; + + //Trim from the end of the string any white space + while(s[s.size() - 1] == ' '){ + s.pop_back(); + } + for(int i = s.size() - 1; i >= 0 ; --i){ + + if(s[i] != ' ' ){ + len += 1; + } + else{ + return len; + } + } + + return len; +} + + + + +int main(){ + cout << "Type words to find length of last word in list: "; + string word = ""; + cin >> word; + cout << lengthOfLastWord(word) << endl; +}