leetcode

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

commit eac3fa9d1469699cf8c625d33b9239b7a1b13787
parent 3068a78f21cced56351e6ae61177b5639ad2dc7b
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Sun, 16 Apr 2023 16:38:28 -0500

Length of last word using dart

Diffstat:
Alength-of-last-word/length-of-last-word.dart | 32++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+), 0 deletions(-)

diff --git a/length-of-last-word/length-of-last-word.dart b/length-of-last-word/length-of-last-word.dart @@ -0,0 +1,32 @@ +import 'dart:io'; + +//This is my first dart program!!! +//I am doing this to start my learning process of flutter +//Runtime: 249ms Beats: 78.21% +//Memory: 140.9 Beats: 42.46% +void main(){ + Solution sol = new Solution(); + stdout.write("Enter words to find length of last word: "); + String input_str = stdin.readLineSync()!; + print(sol.lengthOfLastWord(input_str)); +} + + +class Solution { + int lengthOfLastWord(String s) { + //Clear the spaces from the end of the string + s = s.trim(); + int len = 0; + for(int i = s.length - 1 ; i >= 0; --i){ + if(s[i] == " "){ + break; + } + else{ + len += 1; + } + } + return len; + } +} + +