Skip to content

Commit dfd840a

Browse files
committed
Swap Case
1 parent 6cc868d commit dfd840a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/easy/SwapCase.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package easy;
2+
3+
/**
4+
* Have the function SwapCase(str) take the str parameter
5+
* and swap the case of each character.
6+
* For example: if str is "Hello World" the output should be hELLO wORLD.
7+
* Let numbers and symbols stay the way they are.
8+
*/
9+
public class SwapCase {
10+
11+
/**
12+
* Swap Case function.
13+
*
14+
* @param str input string
15+
* @return the output string
16+
*/
17+
private static String swapCase(String str) {
18+
StringBuilder out = new StringBuilder();
19+
char[] chars = str.toCharArray();
20+
for (char c : chars) {
21+
if (Character.isLowerCase(c)) {
22+
out.append(Character.toUpperCase(c));
23+
} else {
24+
out.append(Character.toLowerCase(c));
25+
}
26+
}
27+
return out.toString();
28+
}
29+
30+
/**
31+
* Entry point.
32+
*
33+
* @param args command line arguments
34+
*/
35+
public static void main(String[] args) {
36+
var result1 = swapCase("The Livin' Free EP");
37+
System.out.println(result1);
38+
var result2 = swapCase("Selected MP3");
39+
System.out.println(result2);
40+
}
41+
42+
}

0 commit comments

Comments
 (0)