Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. /**
  8.  *
  9.  * @author #Panda
  10.  */
  11. public class MyString implements IString {
  12.  
  13.     @Override
  14.     public int f1(String string) {
  15.         String thisStr[] = string.split("\\s+");
  16.         int sum = 0;
  17.         int firstIndex = 0;
  18.         for (int i = 0; i < thisStr.length; i++) {
  19.             boolean check = true;
  20.             for (int j = 0; j < thisStr[i].length(); j++) {
  21.                 if (!Character.isDigit(thisStr[i].charAt(j))) {
  22.                     check = false;
  23.                     break;
  24.                 }
  25.             }
  26.             if (check) {
  27.                 firstIndex = i;
  28.                 for (int num = Integer.parseInt(thisStr[i]); num > 0; num /= 10) {
  29.                     sum += num % 10;
  30.                 }
  31.                 break;
  32.             }
  33.         }
  34.         for(int i = thisStr.length - 1; i > firstIndex; i--)
  35.         {
  36.             boolean check = true;
  37.             for(int j = 0; j < thisStr[i].length(); j++)
  38.             {
  39.                 if (!Character.isDigit(thisStr[i].charAt(j))) {
  40.                     check = false;
  41.                     break;
  42.                 }
  43.             }
  44.             if (check) {
  45.                 for (int num = Integer.parseInt(thisStr[i]); num > 0; num /= 10) {
  46.                     sum += num % 10;
  47.                 }
  48.                 break;
  49.             }
  50.         }
  51.         return sum;
  52.     }
  53.  
  54.     @Override
  55.     public String f2(String string) {
  56.         String s = "";
  57.         int firstIndex = 0;
  58.         String thisStr[] = string.split("\\s+");
  59.         for (int i = 0; i < thisStr.length; i++) {
  60.             boolean check = true;
  61.             for (int j = 0; j < thisStr[i].length(); j++) {
  62.                 if (!Character.isDigit(thisStr[i].charAt(j))) {
  63.                     check = false;
  64.                     break;
  65.                 }
  66.             }
  67.             if (check) {
  68.                 firstIndex = i;
  69.                 StringBuilder s1 = new StringBuilder(thisStr[i]);
  70.                 s += s1.reverse();
  71.                 break;
  72.             }
  73.         }
  74.         s += "\t";
  75.         for(int i = thisStr.length - 1; i > firstIndex; i--)
  76.         {
  77.             boolean check = true;
  78.             for(int j = 0; j < thisStr[i].length(); j++)
  79.             {
  80.                 if (!Character.isDigit(thisStr[i].charAt(j))) {
  81.                     check = false;
  82.                     break;
  83.                 }
  84.             }
  85.             if (check) {
  86.                 StringBuilder s2 = new StringBuilder(thisStr[i]);
  87.                 s += s2.reverse();
  88.                 break;
  89.             }
  90.         }
  91.         return s;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement