Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- System.out.println(textModifier());
- }
- public static String textModifier() {
- String result;
- result = removeExtraSpaces(new Scanner(System.in).nextLine());
- result = swapCharsByMinus(result);
- result = replacePlusesWithExclamationMarks(result);
- return calculateSumm(result);
- }
- private static String removeExtraSpaces(String str) {
- if (str.length() == 0) {
- return str;
- }
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < str.length(); i++) {
- if (str.charAt(i) == ' ' && str.length() > i + 1 && str.charAt(i + 1) == ' ') {
- } else {
- sb.append(str.charAt(i));
- }
- }
- return sb.toString();
- }
- private static String swapCharsByMinus(String str) {
- if (str.length() == 0) {
- return str;
- }
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < str.length(); i++) {
- if (str.charAt(i) == '-' && i != 0 && i + 1 != str.length()) {
- sb.deleteCharAt(sb.length() - 1);
- sb.append(str.charAt(i + 1));
- sb.append(str.charAt(i - 1));
- i += 1;
- } else {
- sb.append(str.charAt(i));
- }
- }
- return sb.toString();
- }
- private static String replacePlusesWithExclamationMarks(String str) {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < str.length(); i++) {
- if (str.charAt(i) == '+') {
- sb.append('!');
- } else {
- sb.append(str.charAt(i));
- }
- }
- return sb.toString();
- }
- private static String calculateSumm(String str) {
- StringBuilder sb = new StringBuilder();
- Integer summ = null;
- char currentChar;
- for (int i = 0; i < str.length(); i++) {
- currentChar = str.charAt(i);
- if (currentChar >= '0' && currentChar <= '9') {
- summ = (summ == null ? (currentChar - '0') : summ + (currentChar - '0'));
- } else {
- sb.append(currentChar);
- }
- }
- if (summ != null) {
- sb.append(' ');
- sb.append(summ);
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement