Skip to content

Commit aa4cd5f

Browse files
committed
reverse snippets
1 parent 7d7d077 commit aa4cd5f

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
Title: Reverse Word Contents
3+
Description: Reverses the characters of each word in a string while preserving word order
4+
Author: Mcbencrafter
5+
Tags: string,reverse,words,transformation,order
6+
---
7+
8+
```java
9+
public static String reverseWords(String text) {
10+
String[] words = text.split("\\s+");
11+
StringBuilder reversedText = new StringBuilder();
12+
13+
for (String word : words) {
14+
StringBuilder reversedWord = new StringBuilder(word).reverse();
15+
reversedText.append(reversedWord).append(" ");
16+
}
17+
18+
return reversedText.toString().trim();
19+
}
20+
21+
// Usage:
22+
System.out.println(reverseWordContents("hello world")); // "olleh dlrow"
23+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
Title: Reverse Words In String
3+
Description: Reverses the order of words in a sentence while preserving the content of each word
4+
Author: Mcbencrafter
5+
Tags: string,reverse,words,transformation,sentence
6+
---
7+
8+
```java
9+
public static String reverseWords(String text) {
10+
String[] words = text.split("\\s+");
11+
StringBuilder reversedSentence = new StringBuilder();
12+
13+
for (int currentWord = words.length - 1; currentWord >= 0; currentWord--) {
14+
reversedSentence.append(words[currentWord]).append(" ");
15+
}
16+
17+
return reversedSentence.toString().trim();
18+
}
19+
20+
// Usage:
21+
System.out.println(reverseWords("hello world")); // Output: world hello
22+
```

0 commit comments

Comments
 (0)