Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Exc6MiddleCharacters {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String text = scanner.nextLine();
- middleChar(text);
- }
- private static void middleChar(String text) {
- int input = text.length();
- if (input % 2 == 1){
- char ch = text.charAt(input/2);
- System.out.println(ch);
- }else {
- char ch1 = text.charAt(input/2-1);
- char ch2 = text.charAt(input/2);
- System.out.printf("%c%c",ch1,ch2);
- }
- }
- }
- //You will receive a single string. Write a method that prints the middle character.
- // If the length of the string is even, there are two middle characters.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement