Skip to content

Commit b4ac617

Browse files
committed
[Manan] ADD:Toggling each character in a string
1 parent 6ae18bb commit b4ac617

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

ToggleCase.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.Scanner;
2+
3+
class ToggleCase {
4+
// Main method
5+
public static void main(String[] args) {
6+
Scanner input = new Scanner(System.in);
7+
8+
System.out.print("Enter a string: ");
9+
String str = input.nextLine();
10+
11+
// Method to toggle character case
12+
String result = toggleCharacterCase(str);
13+
14+
System.out.println("String after toggling character case: " + result);
15+
}
16+
17+
// Method to toggle character case
18+
public static String toggleCharacterCase(String str) {
19+
String result = "";
20+
for (int i = 0; i < str.length(); i++) {
21+
char ch = str.charAt(i);
22+
if((int) ch >=65 && (int) ch <= 90){
23+
result += (char) ((int) ch + 32);
24+
} else if((int) ch >= 97 && (int) ch <= 122){
25+
result += (char) ((int) ch - 32);
26+
} else {
27+
result += ch;
28+
}
29+
}
30+
return result;
31+
}
32+
}

0 commit comments

Comments
 (0)