Exemplo n.º 1
0
 public void cutPlaylist() throws Exception {
   if (logger.isDebugEnabled()) logger.debug("Cut Playlist.");
   String st;
   PrintWriter pw =
       new PrintWriter(new BufferedWriter(new FileWriter(new File(this.NAME + ".dec"))));
   BufferedReader br = new BufferedReader(new FileReader(new File(this.NAME)));
   if ((st = br.readLine()) != null) {
     String st2[];
     st = new File(st).toURL().toString();
     st2 = st.split("/");
     StringBuffer stb = new StringBuffer(st2[st2.length - 1]);
     StringBuffer stb2 = stb.reverse();
     String st3 = new String(stb2);
     int i = 0;
     while (st3.charAt(i) != '.') i++;
     String a = st3.substring(i + 1, st3.length());
     pw.print(new StringBuffer(a).reverse());
   }
   while ((st = br.readLine()) != null) {
     pw.println();
     String st2[];
     st = new File(st).toURL().toString();
     st2 = st.split("/");
     StringBuffer stb = new StringBuffer(st2[st2.length - 1]);
     StringBuffer stb2 = stb.reverse();
     String st3 = new String(stb2);
     int i = 0;
     while (st3.charAt(i) != '.') i++;
     String a = st3.substring(i + 1, st3.length());
     pw.print(new StringBuffer(a).reverse());
   }
   pw.close();
   br.close();
 }
  public static void main(String[] args) throws Exception {

    BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
    int noOfTestCases = Integer.parseInt(scanner.readLine());
    for (int i = 0; i < noOfTestCases; i++) {
      String input = scanner.readLine();
      StringBuffer sb = new StringBuffer(input);
      String reverseString = sb.reverse().toString();
      if (input.equals(reverseString)) {
        System.out.println("-1");
      } else {
        for (int j = 0; j < input.length(); j++) {
          if (input.charAt(j) != reverseString.charAt(j)) {
            String inputStringSubstring = input.substring(0, j) + input.substring(j + 1);
            String inputStringSubstringReverse =
                new StringBuffer(inputStringSubstring).reverse().toString();
            //						String reverseStringSubstring=reverseString.substring(0,j)+input.substring(j+1);
            //						String reverseStringSubstringReverse=new
            // StringBuffer(reverseStringSubstring).reverse().toString();
            if (inputStringSubstring.equals(inputStringSubstringReverse)) {
              System.out.println(j);
            } else {
              System.out.println(input.length() - 1 - j);
            }
            break;
          }
          continue;
        }
      }
    }
  }
Exemplo n.º 3
0
  public void test_reverse() {
    StringBuffer buff = new StringBuffer();
    harness.check(!(!buff.reverse().toString().equals("")), "test_reverse - 1");

    buff = new StringBuffer("babu");
    harness.check(!(!buff.reverse().toString().equals("ubab")), "test_reverse - 2");

    buff = new StringBuffer("malayalam");
    harness.check(!(!buff.reverse().toString().equals("malayalam")), "test_reverse - 3");

    buff = new StringBuffer("cnbcbnc");
    harness.check(!(!buff.reverse().toString().equals("cnbcbnc")), "test_reverse - 4");

    buff = new StringBuffer("vinod");
    harness.check(!(!buff.reverse().toString().equals("doniv")), "test_reverse - 5");
  }
Exemplo n.º 4
0
  /**
   * Segments the paragraph to sentences according to currently setup rules.
   *
   * <p>Bugfix for <a href="http://sourceforge.net/support/tracker.php?aid=1288742">issue
   * 1288742</a>: Sentences are returned without spaces in the beginning and at the end of a
   * sentence.
   *
   * <p>An additional list with space information is returned to be able to glue translation
   * together with the same spaces between them as in original paragraph.
   *
   * @param paragraph the paragraph text
   * @param spaces list to store information about spaces between sentences
   * @param brules list to store rules that account to breaks
   * @return list of sentences (String objects)
   */
  public static List<String> segment(
      Language lang, String paragraph, List<StringBuffer> spaces, List<Rule> brules) {
    List<String> segments = breakParagraph(lang, paragraph, brules);
    List<String> sentences = new ArrayList<String>(segments.size());
    if (spaces == null) spaces = new ArrayList<StringBuffer>();
    spaces.clear();
    for (String one : segments) {
      int len = one.length();
      int b = 0;
      StringBuffer bs = new StringBuffer();
      while (b < len && Character.isWhitespace(one.charAt(b))) {
        bs.append(one.charAt(b));
        b++;
      }

      int e = len - 1;
      StringBuffer es = new StringBuffer();
      while (e >= b && Character.isWhitespace(one.charAt(e))) {
        es.append(one.charAt(e));
        e--;
      }
      es.reverse();

      String trimmed = one.substring(b, e + 1);
      sentences.add(trimmed);
      if (spaces != null) {
        spaces.add(bs);
        spaces.add(es);
      }
    }
    return sentences;
  }
Exemplo n.º 5
0
  // 加密方法
  public static String encrypt(String strOriginal) {
    BASE64Encoder encoder = new BASE64Encoder();

    char charTemp;
    int intCrpytion; //
    int intRnd; // 加密随机数
    intRnd = (int) (100 * Math.random() + 1);
    StringBuffer strOriToB = new StringBuffer(strOriginal); // 将String参数转换为StringBuffer

    /*
     * 加密算法:将字符串先进行反转,对每个字符强制取整后和产生的随机数进行按位异或,再把随机数对应的字符连接到字符串的尾部。
     */
    StringBuilder resultBuilder = new StringBuilder("");
    strOriToB = strOriToB.reverse(); // 反转字符串
    for (int i = 0; i < strOriToB.length(); i++) {
      charTemp = strOriToB.charAt(i);
      intCrpytion = (int) charTemp;
      intCrpytion = intCrpytion ^ (intRnd % 32);
      resultBuilder.append((char) intCrpytion);
    }
    resultBuilder.append((char) intRnd);

    byte[] byteArr = null;
    try {
      byteArr = resultBuilder.toString().getBytes(charset);
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    return encoder.encode(byteArr);
  }
Exemplo n.º 6
0
  public String addBinary(String a, String b) {
    int ap = a.length() - 1;
    int bp = b.length() - 1;
    StringBuffer sb = new StringBuffer();
    flag = false;
    while (ap >= 0 && bp >= 0) {
      sb.append(add(a.charAt(ap), b.charAt(bp)));
      ap--;
      bp--;
    }
    if (ap < 0 && bp < 0) {

    } else if (ap < 0) {
      while (bp >= 0) {
        sb.append(add(b.charAt(bp), '0'));
        bp--;
      }
    } else {
      while (ap >= 0) {
        sb.append(add(a.charAt(ap), '0'));
        ap--;
      }
    }

    if (flag) sb.append('1');

    return sb.reverse().toString();
  }
Exemplo n.º 7
0
 // 解密方法
 public static String decrypt(String strCryptograph) throws Exception {
   try {
     if (StringUtil.isEmpty(strCryptograph)) {
       return strCryptograph;
     }
     BASE64Decoder decoder = new BASE64Decoder();
     strCryptograph = new String(decoder.decodeBuffer(strCryptograph, charset), charset);
     String strReturn = new String();
     int intTemp;
     int intCrypt;
     String strTemp = new String();
     intTemp = (int) strCryptograph.charAt(strCryptograph.length() - 1);
     strTemp = strCryptograph.substring(0, strCryptograph.length() - 1);
     for (int i = 0; i < strTemp.length(); i++) {
       intCrypt = (int) strTemp.charAt(i);
       intCrypt = intCrypt ^ (intTemp % 32);
       strReturn += (char) intCrypt;
     }
     StringBuffer strRe = new StringBuffer(strReturn);
     strRe = strRe.reverse(); // 反转字符串
     return strRe.toString();
   } catch (Exception e) {
     log.error("解密字符串失败!" + strCryptograph + "\n" + e.getMessage(), e);
     throw e;
   }
 }
Exemplo n.º 8
0
  /**
   * Description of the Method
   *
   * @param s Description of the Parameter
   * @return Description of the Return Value
   */
  protected Element createContent(WebSession s) {
    ElementContainer ec = new ElementContainer();

    StringBuffer person = null;
    try {
      ec.addElement(new StringElement(WebGoatI18N.get("EnterYourName") + ": "));

      person = new StringBuffer(s.getParser().getStringParameter(PERSON, ""));
      person.reverse();

      Input input = new Input(Input.TEXT, PERSON, person.toString());
      ec.addElement(input);

      Element b = ECSFactory.makeButton(WebGoatI18N.get("Go!"));
      ec.addElement(b);
    } catch (Exception e) {
      s.setMessage("Error generating " + this.getClass().getName());
      e.printStackTrace();
    }

    if (!person.toString().equals("") && getLessonTracker(s).getNumVisits() > 3) {
      makeSuccess(s);
    }

    return (ec);
  }
Exemplo n.º 9
0
  /**
   * Convert decimal to hexadecimal�iNo limit string length�j <br>
   * Return hexadecimal string (unsigned integer number string) from decimal(integer number) <br>
   * In case parameter is a minus number, return blank string <br>
   *
   * @param argStr decimal string
   * @return hexadecimal string
   */
  public static String chgHexString(String argStr) {

    // In case parameter is a minus number, return blank string
    if (argStr.charAt(0) == '-') {
      return "";
    }

    StringBuffer hexb = new StringBuffer();
    BigInteger bi = new BigInteger(argStr);
    int tempInt;
    BigInteger tempBi;

    // Convert each 4 bit, start from the end.
    for (int bitlength = bi.bitLength(); bitlength > 0; bitlength -= 4) {
      tempBi = bi.and(HEX_MASK);
      tempInt = tempBi.intValue();
      hexb.append(HEX_STR.charAt(tempInt));
      bi = bi.shiftRight(4);
    }
    // correction after converting
    int hexlength = hexb.length();
    if (hexlength == 0) {
      // In case value =0, put "00"
      hexb.append("00");
    } else if ((hexlength % 2) == 1) {
      // After converting, if result is a old number string, add "0" at
      // the end of string
      hexb.append("0");
    }

    // Reverse result string (because string was converted from the
    // 4-bit-end)
    return hexb.reverse().toString();
  }
Exemplo n.º 10
0
 public void checkPalindrome(String str) {
   StringBuffer strBuff = new StringBuffer(str);
   if (str.equalsIgnoreCase(strBuff.reverse().toString())) {
     System.out.println(str + " is a palindrome");
   } else {
     System.out.println(str + " is not a palindrome");
   }
 }
Exemplo n.º 11
0
 /**
  * Create an escaped Unicode character
  *
  * @param c
  * @return The unicode character in escaped string form
  */
 private static String escape(char c) {
   StringBuffer b = new StringBuffer();
   for (int i = 0; i < 4; i++) {
     b.append(Integer.toHexString(c & 0x000F).toUpperCase());
     c >>>= 4;
   }
   b.append("u\\");
   return b.reverse().toString();
 }
Exemplo n.º 12
0
 public static void print_binary(int n) {
   String binaryNum = "";
   while (n >= 1) {
     binaryNum += n % 2;
     n = n / 2;
   }
   StringBuffer num = new StringBuffer(binaryNum);
   System.out.println(num.reverse().toString());
 }
Exemplo n.º 13
0
  public static void main(String[] args) {
    //  P439
    //  Using StringBuilder and StringBuffer
    String x1 = "abc";
    x1.concat("def");
    System.out.println("x1 = " + x1); //  output is "x1 = abc"
    String x2 = "abc";
    x2 = x2.concat("def");
    System.out.println("x2 = " + x2); //  output is "x2 = abcdef"
    StringBuffer sb1 = new StringBuffer("abc");
    sb1.append("def");
    System.out.println("sb1 = " + sb1); //  output is "sb1 = abcdef"
    //  P440
    StringBuilder sb2 = new StringBuilder("abc");
    sb2.append("def").reverse().insert(3, "---");
    System.out.println(sb2); //  output is "fed---cba"

    //  public synchronized StringBuffer append(String s)
    StringBuffer sb3 = new StringBuffer("set ");
    sb3.append("point");
    System.out.println(sb3); //  output is "set point"
    StringBuffer sb4 = new StringBuffer("pi = ");
    sb4.append(3.14159f);
    System.out.println(sb4); //  output is "pi = 3.14159"

    //  public StringBuilder delete(int start,int end)
    StringBuilder sb5 = new StringBuilder("0123456789");
    System.out.println(sb5.delete(4, 6)); //  output is "01236789"

    //  P441
    //  Exam watch!
    StringBuffer sb6 = new StringBuffer("abc");
    sb6.append("def");
    System.out.println(sb6);

    //  public StringBuilder insert(int offset,String s)
    StringBuilder sb7 = new StringBuilder("01234567");
    sb7.insert(4, "---");
    System.out.println(sb7); //  output is "0123---4567"

    //  public synchronized StringBuffer reverse()
    StringBuffer sb8 = new StringBuffer("A man a plan a canal Panama");
    sb8.reverse();
    System.out.println(sb8); //  output: "amanaP lanac a nalp a nam A"

    //  public String toString()
    StringBuffer sb9 = new StringBuffer("test string");
    System.out.println(sb9.toString()); //  output is "test string"

    //  P442
    // result = method1().method2().method3();
    String x = "abc";
    String y = x.concat("def").toUpperCase().replace('C', 'x');
    //  chained methods
    System.out.println("y = " + y); //  result is "y = ABxDEF"
  }
Exemplo n.º 14
0
 /** This method reverses the Strings for all stored loop motifs */
 public void reverseStrings() {
   for (int i = 0; i < 8; i++) {
     if (this.loopmotifs[i] != null) {
       StringBuffer st = new StringBuffer(this.loopmotifs[i]);
       st.reverse();
       this.loopmotifs[i] = st.toString();
     }
   }
   updateInfo();
 }
Exemplo n.º 15
0
 // Read base 10 number LS to MS digit and convert each digit to binary via %2
 // Reverse string to order MS to LS
 public static String convertIntToBinaryString(int n) {
   StringBuffer sb = new StringBuffer();
   if (n == 0) {
     return "0";
   }
   while (n > 0) {
     sb.append(n % 2); // Get LS digit of current number
     n >>= 1; // Shift current number right by one digit
   }
   return sb.reverse().toString();
 };
Exemplo n.º 16
0
 /**
  * @param n: Given a decimal number that is passed in as a string
  * @return: A string
  */
 public String IntToBinary(int cur) {
   StringBuffer sb = new StringBuffer();
   if (cur == 0) return "0";
   while (cur != 0) {
     int temp = cur % 2;
     sb.append(String.valueOf(temp));
     cur = cur / 2;
   }
   sb.reverse();
   return sb.toString();
 }
Exemplo n.º 17
0
  /**
   * Converts a int value to a hexadecimal String of length 8. Includes leading zeros if necessary.
   *
   * @param value The int value to be converted.
   * @return The hexadecimal string representation of the int value.
   */
  public static String toFullHexString(int value) {
    int currentValue = value;
    StringBuffer stringBuffer = new StringBuffer(8);
    for (int i = 0; i < 8; i++) {
      int currentDigit = currentValue & 0xf;
      stringBuffer.append(HEX_DIGITS[currentDigit]);
      currentValue >>>= 4;
    }

    return stringBuffer.reverse().toString();
  }
Exemplo n.º 18
0
  /**
   * Converts a long value to a hexadecimal String of length 16. Includes leading zeros if
   * necessary.
   *
   * @param value The long value to be converted.
   * @return The hexadecimal string representation of the long value.
   */
  public static String toFullHexString(long value) {
    long currentValue = value;
    StringBuffer stringBuffer = new StringBuffer(16);
    for (int j = 0; j < 16; j++) {
      int currentDigit = (int) currentValue & 0xf;
      stringBuffer.append(HEX_DIGITS[currentDigit]);
      currentValue >>>= 4;
    }

    return stringBuffer.reverse().toString();
  }
Exemplo n.º 19
0
  /**
   * Description of the Method
   *
   * @param value Description of the Parameter
   * @return Description of the Return Value
   */
  private String encode(String value) {
    // <START_OMIT_SOURCE>
    StringBuffer encoded = new StringBuffer();

    for (int i = 0; i < value.length(); i++) {
      encoded.append(String.valueOf((char) (value.charAt(i) + 1)));
    }

    return encoded.reverse().toString();
    // <END_OMIT_SOURCE>
  }
Exemplo n.º 20
0
  static String fromIntToAlphabet(int value, String alphabet) {
    StringBuffer ans = new StringBuffer();
    int base = alphabet.length();

    while (value > 0) {
      ans.append(alphabet.charAt(value % base));
      value /= base;
    }

    return ans.reverse().toString();
  }
 /**
  * 格式化输出尾部信息内容
  *
  * @param len
  * @return
  */
 private static String formatFootString(int len) {
   // 將頭部內容反轉即可
   String head = formatHeadString(len);
   if (head != null) {
     StringBuffer tmp = new StringBuffer(head);
     String s = tmp.reverse().toString();
     tmp = null;
     return s;
   }
   return null;
 }
Exemplo n.º 22
0
  /**
   * Returns a string containing the characters on the path from this node to the root, but not
   * including the root character. The last character in the returned string is the character at
   * this node.
   *
   * @return String.
   */
  public String toString() {
    StringBuffer sb = new StringBuffer(getHeight());
    Trie t = this;

    while (t.parent != null) {
      sb.append(t.ch);
      t = t.parent;
    }

    return sb.reverse().toString();
  }
Exemplo n.º 23
0
 public void execute(Job job) {
   // Get the input as a String
   String data = ByteUtils.fromBytes(job.getData(), encoding);
   // Perform the reversal
   StringBuffer sb = new StringBuffer(data);
   sb = sb.reverse();
   // Store result as bytes
   job.setResult(ByteUtils.toBytes(sb.toString(), encoding));
   // Set the job state
   job.setState(JobState.COMPLETE);
 }
Exemplo n.º 24
0
 public static String getArtistName(String st) throws Exception {
   st = new File(st).toURL().toString();
   String st2[] = st.split("/");
   StringBuffer stb = new StringBuffer(st2[st2.length - 1]);
   StringBuffer stb2 = stb.reverse();
   String st3 = new String(stb2);
   int i = 0;
   while (st3.charAt(i) != '.') i++;
   String a = st3.substring(i + 1, st3.length());
   if (a.length() >= 21) a = "..." + a.substring(a.length() - 21, a.length());
   return (new String(new StringBuffer(a).reverse()));
 }
Exemplo n.º 25
0
 public static String toShortString(long id) {
   if (id < 0) {
     throw new IllegalArgumentException("id = " + id);
   }
   StringBuffer buffer = new StringBuffer();
   while (id > 0) {
     buffer.append(getBaseString().charAt((int) (id % 62)));
     id /= 62;
   }
   System.out.println("before : " + buffer.toString());
   return buffer.reverse().toString();
 }
Exemplo n.º 26
0
 /**
  * Metodo que obtiene el nombre de un archivo eliminando la ruta
  *
  * @author Edgar Joao
  * @param name
  * @param character
  * @return
  */
 public static String getFileName(String name) {
   StringBuffer result = null;
   result = new StringBuffer(0);
   for (int i = name.length() - 1; i >= 0; i--) {
     if (name.charAt(i) != '/') {
       result.append(name.charAt(i));
     } else {
       break;
     }
   }
   result.reverse();
   return result.toString();
 }
Exemplo n.º 27
0
 // Convert integer to Excel code (1-based)
 // Ex. 1 = "A", 53 = "BA" (2x26^1 + 1x26^0)
 public static String convertIntToExcelCode(int n) {
   StringBuffer sb = new StringBuffer();
   while (n > 0) {
     // Fetch least significant digit and convert to character code
     int cCode = n % 26 + 65 - 1; // "A" starts from 65
     // Append code to results
     sb.append((char) cCode);
     // Shift integer right by one digit (base 26)
     n /= 26;
   }
   // Results were constructed from least to most significant codes, so need to reverse
   return sb.reverse().toString();
 }
Exemplo n.º 28
0
 public String shift(String s) {
   int d = 0;
   StringBuffer sb = new StringBuffer();
   for (int i = s.length() - 1; i >= 0; i--) {
     int temp = s.charAt(i) - '0';
     sb.append(String.valueOf((temp * 2 + d) % 10));
     d = (temp * 2 + d) / 10;
   }
   // while (sb.charAt(index) == '0') index ++;
   s = sb.reverse().toString();
   int index = sb.length() - 1;
   while (index >= 0 && s.charAt(index) == '0') index--;
   s = s.substring(0, index + 1);
   return s;
 }
Exemplo n.º 29
0
 public static String addBinary(String a, String b) {
   if (a.length() == 0 && b.length() == 0) {
     return a.length() == 0 ? b : a;
   }
   int m = a.length() - 1, n = b.length() - 1;
   int abit, bbit, result, carry = 0;
   StringBuffer br = new StringBuffer();
   while (m >= 0 || n >= 0 || carry == 1) {
     abit = (m >= 0) ? (a.charAt(m--) - '0') : 0;
     bbit = (n >= 0) ? Character.getNumericValue(b.charAt(n--)) : 0;
     result = abit ^ bbit ^ carry;
     carry = (abit + bbit + carry) / 2;
     br.append(result);
   }
   return br.reverse().toString();
 }
Exemplo n.º 30
0
  public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringBuffer sb1 = new StringBuffer();
    StringBuffer sb2 = new StringBuffer();

    String st = br.readLine();
    int n = Integer.valueOf(br.readLine());

    Stack<Character> s1 = new Stack<>();
    Stack<Character> s2 = new Stack<>();

    for (int i = 0; i < st.length(); i++) {
      s1.add(st.charAt(i));
    }

    char c;
    StringTokenizer t;
    for (int i = 0; i < n; i++) {
      t = new StringTokenizer(br.readLine());
      c = t.nextToken().charAt(0);
      switch (c) {
        case 'P':
          char x = t.nextToken().charAt(0);
          s1.add(x);
          break;
        case 'L':
          if (!s1.isEmpty()) s2.add(s1.pop());
          break;
        case 'B':
          if (!s1.isEmpty()) s1.pop();
          break;
        case 'D':
          if (!s2.isEmpty()) s1.add(s2.pop());
          break;
      }
    }
    int s1Len = s1.size();
    for (int i = 0; i < s1Len; i++) {
      sb1.append(s1.pop());
    }
    int s2Len = s2.size();
    for (int i = 0; i < s2Len; i++) {
      sb2.append(s2.pop());
    }
    System.out.println(sb1.reverse().toString() + sb2.toString());
  }