forked from sourabh48/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringReverse.java
More file actions
29 lines (23 loc) · 830 Bytes
/
StringReverse.java
File metadata and controls
29 lines (23 loc) · 830 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class StringReverse {
public static void main(String[] args) {
String sentence = "Hello";
String reversedSentence = reverseWords(sentence);
System.out.println(reversedSentence);
}
public static String reverseWords(String sentence) {
String[] words = sentence.split("\\b");
StringBuilder reversedSentence = new StringBuilder();
for (int i = 0; i < words.length; i++) {
words[i] = reverseString(words[i]);
reversedSentence.append(words[i]);
}
return reversedSentence.toString();
}
public static String reverseString(String word) {
String reversedWord = "";
for (int i = word.length() - 1; i >= 0; i--) {
reversedWord += word.charAt(i);
}
return reversedWord;
}
}