length-of-last-word.dart (698B)
1 import 'dart:io'; 2 3 //This is my first dart program!!! 4 //I am doing this to start my learning process of flutter 5 //Runtime: 249ms Beats: 78.21% 6 //Memory: 140.9 Beats: 42.46% 7 void main(){ 8 Solution sol = new Solution(); 9 stdout.write("Enter words to find length of last word: "); 10 String input_str = stdin.readLineSync()!; 11 print(sol.lengthOfLastWord(input_str)); 12 } 13 14 15 class Solution { 16 int lengthOfLastWord(String s) { 17 //Clear the spaces from the end of the string 18 s = s.trim(); 19 int len = 0; 20 for(int i = s.length - 1 ; i >= 0; --i){ 21 if(s[i] == " "){ 22 break; 23 } 24 else{ 25 len += 1; 26 } 27 } 28 return len; 29 } 30 } 31 32