I am trying to solve this issue in so many times. But I can't understand how to get numeric value respected ASCII character.
By following this steps:
Input string:--->
?85O89*69R65*87O104*33I104
Output should be like this:--->
?uoy*era*woH*!iH
How I get the output In java?
Unlike the others I think this is actually an interesting question, that a newbie may find difficult to understand.
Given the string
?85O89*69R65*87O104*33I104
We see there is repetitive pattern, a character + a numeric value. We have then
?85
O89
*69
R65
*87
O104
*33
I104
Based on the proposed output we understand the number represents an ASCII codepoint .
Also we see that the case of each character is reversed.
Thus, we may write the following code.
final String value = "?85O89*69R65*87O104*33I104";
final StringBuilder sb = new StringBuilder();
// We split the string based on the \w(\d+) pattern, "reversed"
final String[] parts = value.split("(?=([^\\d]+))");
for (final String s : parts) {
// For each character we must reverse its case
// The first symbol is already in a presentable form
final char c = s.charAt(0);
sb.append(Character.isUpperCase(c) ? Character.toLowerCase(c) : Character.toUpperCase(c));
// The remaining part represents an ASCII symbol in decimal form
final int codePoint = Integer.parseInt(s.substring(1));
final String str = new String(Character.toChars(codePoint));
sb.append(Character.isUpperCase(codePoint) ? str.toLowerCase() : str.toUpperCase());
}
System.out.println(sb);
Which will correctly print
?uoy*era*woH*!iH
The input string seems to be using *
as "word" delimiters which should be kept in the output.
A "word" contains 3 characters, the first and the last characters are displayed as ASCII codes in the encoded string.
When decoding a word, the case of the letter character has to be inverted: lower -> upper, upper -> lower.
That being said, the following implementation may be as follows:
static Pattern WORD = Pattern.compile("(\\d+)(.)(\\d+)");
public static String decode(String str) {
return Arrays
.stream(str.split("((?<=[*!\\?])|(?=[*!\\?]))"))
.map(s -> s.matches(WORD.pattern()) ? decodeWord(s) : s)
.collect(Collectors.joining());
}
private static String decodeWord(String word) {
Matcher match = WORD.matcher(word);
if (match.find()) {
return new StringBuilder()
.append(decodeChar((char)(Integer.parseInt(match.group(1)))))
.append(decodeChar(match.group(2).charAt(0)))
.append(decodeChar((char)(Integer.parseInt(match.group(3)))))
.toString();
}
return "";
}
static char decodeChar(char c) {
if (Character.isLetter(c)) {
return Character.isUpperCase(c) ? Character.toLowerCase(c) : Character.toUpperCase(c);
}
return c;
}
Test:
System.out.println(decode("?85O89*69R65*87O104*33I104"));
Output:
?uoy*era*woH*!iH
According to what you said, and base on the string you give, I'm ASSUMING that every even position is between(not include the ending) two non-numbers.
public static String perform(String str) {
StringBuilder sb = new StringBuilder("");
String dump = "";//dump string
for(int i = 0;i<str.length();i++) {//Read the string char by char
int ascii = (int) str.charAt(i);//work with ASCII
if(ascii - '0' < 0 || ascii - '0' > 9) {//check if ascii is a non-number
if(dump.length() != 0) {//can dump
int c = Integer.parseInt(dump);
if(c >= 65 && c <= 90) {//uppercase to lowercase
sb.append((char) (c - 'A' + 'a'));
}
if(c >= 97 && c <= 122) {//lowercase to uppercase
sb.append((char) (c - 'a' + 'A'));
}
dump = "";//clear
}
if(ascii < 65 || (ascii > 90 && ascii < 97) || ascii > 122) {//not a letter
sb.append(str.charAt(i));
}else {//is a letter
if(ascii >= 65 && ascii <= 90) {//uppercase to lowercase
sb.append((char) (ascii - 'A' + 'a'));
}
if(ascii >= 97 && ascii <= 122) {//lowercase to uppercase
sb.append((char) (ascii - 'a' + 'A'));
}
}
}else {// Is number
dump += str.charAt(i);
}
}
if(dump.length() != 0) {
int c = Integer.parseInt(dump);
if(c >= 65 && c <= 90) {//uppercase to lowercase
sb.append((char) (c - 'A' + 'a'));
}
if(c >= 97 && c <= 122) {//lowercase to uppercase
sb.append((char) (c - 'a' + 'A'));
}
}
return sb.toString();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.