Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1012ff7

Browse files
authored
ReverseWord & ReverseWordsTest (TheAlgorithms#2022)
* ReverseWord & ReverseWordsTest* ReverseWord & ReverseWordsTest* fixed ReverseWords* Merge branch 'master' ofhttps://github.com/TheAlgorithms/Java into Development
1 parent546bc3f commit1012ff7

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
packagecom.strings;
2+
3+
publicclassReverseWords {
4+
/**
5+
* Converts all of the words in this {@code String} to reversed words
6+
*
7+
* @param s the string to convert
8+
* @return the {@code String}, converted to a string with reveresed words.
9+
*/
10+
11+
publicStringreturnReverseWords(Strings) {
12+
StringBuildersb =newStringBuilder();
13+
StringBuilderword =newStringBuilder();
14+
15+
for(inti =0;i <s.length();i++) {
16+
charc =s.charAt(i);
17+
if(c ==' ') {
18+
19+
sb.append(word);
20+
sb.append(" ");
21+
word.setLength(0);
22+
continue;
23+
}
24+
word.insert(0,c);
25+
}
26+
sb.append(word);
27+
28+
returnsb.toString();
29+
}
30+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
packagecom.string;
2+
3+
importorg.junit.jupiter.api.Assertions;
4+
importorg.junit.jupiter.api.Test;
5+
6+
classReveresWordsTest {
7+
8+
@Test
9+
voidtestReverseWords() {
10+
ReverseWordsreverseWords =newReverseWords();
11+
Assertions.assertEquals(true,reverseWords.returnReverseWords("this is my car"),"siht si ym rac");
12+
Assertions.assertEquals(true,reverseWords.returnReverseWords("ABC 123"),"CBA 321");
13+
14+
}
15+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp