Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- public static void main(String[] args) {
- String S1 = "ABCD"; //println
- //output char one by one from String S1 forward
- //sample output
- //A
- //B
- //C
- //D
- //output char one by one from Strign S1 backward
- //sample output
- //D
- //C
- //B
- //A
- String S2 = "EFGH";
- //output char one by one from S1 and S2
- //alternatively forward in the same line (print)
- //sample output: AEBFCGDH
- //output char one by one from S1 and S2
- //alternatively backward in the same line (print)
- //sample output: HDGCFBEA
- for (int i=0; i<S1.length(); i++){
- System.out.println(S1.charAt(i));
- }
- for (int i=S1.length()-1; i>=0; i--){
- System.out.println(S1.charAt(i));
- }
- for (int i=0; i<S1.length(); i++){
- System.out.print(S1.charAt(i));
- System.out.print(S2.charAt(i));
- }
- System.out.println();
- for (int i=S1.length()-1; i>=0; i--){
- System.out.print(S2.charAt(i));
- System.out.print(S1.charAt(i));
- }
- System.out.println();
- //Final
- //output char one by one from S1 and S2
- //S1 forward
- //S2 backward
- //alternatively in the same line (print)
- //sample output: AHBGCFDE
- for (int i=0; i<S1.length(); i++){ //forward loop
- System.out.print(S1.charAt(i));
- System.out.print(S2.charAt(S1.length()-1-i));
- //last index-1 can get backward index
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement