-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggleChar.java
More file actions
24 lines (24 loc) · 1.04 KB
/
toggleChar.java
File metadata and controls
24 lines (24 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Question: Toggle the characters of a string (uppercase characters-> lowercase character & lowercase char-> uppercase char)
package stringBuilder;
import java.util.Scanner;
public class toggleChar {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the String: ");
StringBuilder sb = new StringBuilder(scan.nextLine());
for (int i = 0; i < sb.length(); i++) {
char ch = sb.charAt(i);
int Ascii = (int) ch; // Type casting (char-> int)
if(Ascii>=65 && Ascii<=90){ // Capital
Ascii += 32; // we have to add ascii value 32 to get lower character
ch = (char) Ascii; // Type casting (int-> char)
sb.setCharAt(i, ch);
}else{ // Lowercase
Ascii = Ascii - 32;
ch = (char) Ascii;
sb.setCharAt(i, ch);
}
}
System.out.println("After toggling: " + sb);
}
}