import java.util.Stack; import java.util.StringTokenizer; public class reverse { public static void main(String[] argv) { //Using StringBuilder reverse() String sh = "Hello World 2020"; System.out.println(sh + " -> " + new StringBuilder(sh).reverse()); //Hello World 2020 -> 0202 dlroW olleH //Using Stack & StringTokenizer pushing and poping whole word String s = "Hello World 2020"; Stack<String> myStack = new Stack<>(); StringTokenizer st = new StringTokenizer(s); while (st.hasMoreTokens()) { myStack.push(st.nextToken()); } // Print the stack backwards System.out.print('"' + s + '"' + " backwards by word is:\n\t\""); while (!myStack.empty()) { System.out.print(myStack.pop()); System.out.print(' '); } System.out.println('"'); //"Hello World 2020" backwards by word is: //"2020 World Hello " } }
Tuesday, May 5, 2020
Reversing a String by Word or by Character
You wish to reverse a string, a character, or a word at a time.
You can reverse a string by character easily, using a StringBuilder.
One natural way is to use a StringTokenizer and a stack. Stack is a class that implements an easy-to-use last-in, first-out (LIFO)
Monday, May 4, 2020
Converting Between Unicode Characters and Strings
Unicode is an international standard that aims to represent all known characters used by people in their various languages
Java Strings Part 3
Java Strings Part 2
Java Strings Part 1
public class UnicodeChars { public static void main(String[] argv) { StringBuffer b = new StringBuffer(); for (char c = 'a'; c < 'd'; c++) { b.append(c); } b.append('\u00a5');// Japanese Yen symbol b.append('\u01FC');// Roman AE with acute accent b.append('\u0391');// GREEK Capital Alpha b.append('\u03A9');// GREEK Capital Omega b.append('\u00E1');// á for (int i = 0; i < b.length(); i++) { System.out.printf( "Character #%d (%04x) is %c%n", i, (int) b.charAt(i), b.charAt(i)); } System.out.println("Accumulated characters are " + b); } } //Character #0 (0061) is a //Character #1 (0062) is b //Character #2 (0063) is c //Character #3 (00a5) is ¥ //Character #4 (01fc) is Ǽ //Character #5 (0391) is Α //Character #6 (03a9) is Ω //Character #7 (00e1) is á //Accumulated characters are abc¥ǼΑΩá
Java Strings Part 2
Java Strings Part 1
Subscribe to:
Comments (Atom)
-
In this post we will use the following docker container: https://hub.docker.com/r/kristophjunge/test-saml-idp/ Also check Microsoft Docume...
-
You wish to reverse a string, a character, or a word at a time. You can reverse a string by character easily, using a StringBuilder. One na...