Skip to content

Commit 984583e

Browse files
authored
Add Longest Word in Dart (#4978)
1 parent f1cae0f commit 984583e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

archive/d/dart/longest-word.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//Issue 4972
2+
3+
void main(List<String> argv){
4+
const String error_message = "Usage: please provide a string";
5+
if (argv.isEmpty || argv[0].isEmpty){
6+
print(error_message);
7+
return;
8+
}
9+
10+
String sentence = argv[0];
11+
String raw = sentence.replaceAll(r'\t', '\t').replaceAll(r'\r', '\r').replaceAll(r'\n', '\n');
12+
List<String> words = raw.split(RegExp(r'\s+'));
13+
int max_length= 0;
14+
15+
for (String each_word in words){
16+
if (each_word.length > max_length){
17+
max_length = each_word.length;
18+
}
19+
}
20+
print(max_length);
21+
}

0 commit comments

Comments
 (0)