Skip to content

Commit ea96f63

Browse files
committed
finding snippets
1 parent 0e1bf3a commit ea96f63

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Find Longest Word
3+
description: Returns the longest word in a string
4+
author: Mcbencrafter
5+
tags: string,length,words
6+
---
7+
8+
```java
9+
public static String findLongestWord(String text) {
10+
String[] words = text.split("\\s+");
11+
String longestWord = words[0];
12+
13+
for (String word : words) {
14+
if (word.length() <= longestWord.length())
15+
continue;
16+
17+
longestWord = word;
18+
}
19+
20+
return longestWord;
21+
}
22+
23+
// Usage:
24+
System.out.println(findLongestWord("hello world123")); // "world123"
25+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
Title: Find Unique Characters
3+
Description: Returns a set of unique characters from a string, with options to include spaces and control case sensitivity
4+
Author: Mcbencrafter
5+
Tags: string,unique,characters,case-sensitive
6+
---
7+
8+
```java
9+
public static Set<Character> findUniqueCharacters(String text, boolean countSpaces, boolean caseSensitive) {
10+
Set<Character> uniqueCharacters = new TreeSet<>();
11+
12+
for (char character : text.toCharArray()) {
13+
if (character == ' ' && !countSpaces)
14+
continue;
15+
if (!caseSensitive)
16+
character = Character.toLowerCase(character);
17+
uniqueCharacters.add(character);
18+
}
19+
20+
return uniqueCharacters;
21+
}
22+
23+
// Usage:
24+
System.out.println(findUniqueCharacters("hello world", false, true)); // Output: [d, e, h, l, o, r, w]
25+
```

0 commit comments

Comments
 (0)