Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. /****************************************************************************************************************
  2.  Program: loops.java - This program performs word-in-a-box string mutations via loops.
  3.  Author: Dave Aldrich
  4.  Date: March 20, 2018
  5.  ***************************************************************************************************************/
  6.  
  7.  
  8. import java.lang.*;
  9. import java.util.*;
  10.  
  11. public class loops {
  12.     public static void main(String[] args){
  13.         Scanner scan = new Scanner(System.in);
  14.         String inputStr = "";    // user input
  15.         String lineStore = "";   // used for any arbituary length of string
  16.         String outputStr = "";   // output string after mutations
  17.         String spaceStr = " ";   // whitespace string; length determined by spaceStore ***see the 2nd spaceStr doc***
  18.         int spaceStore = 0;     // stores the number of whitespaces in a mutated string, -2 because the last space
  19.                                  // and character aren't needed
  20.        
  21.        
  22.         p("Enter a string: "+inputStr);
  23.         inputStr = scan.nextLine();
  24.         lineStore = inputStr.toUpperCase();
  25.        
  26.         int r=lineStore.length();        // r stands for range
  27.        
  28.         for(int i=0; i < r; i++){    
  29.             outputStr = lineStore.charAt(i) + " ";
  30.             p(outputStr);
  31.             spaceStore++;            // this will count how many spaces added    
  32.         }
  33.        
  34.        
  35.         /*
  36.           Makes another spaceStr obj but with repeated whitespaces using replace method.
  37.           To clear up: the string is created using an array of null chars, but the nulls
  38.           are replaced with the whitespace which was used to initialize spaceStr.
  39.         */
  40.        
  41.         spaceStr = new String(new char[spaceStore*2-3]).replace("\0", spaceStr);
  42.        
  43.         for(int i=0; i < r-1; i++){
  44.             if(i != 0){
  45.                 outputStr = lineStore.charAt(i)+spaceStr+lineStore.charAt(r - i-1);    // takes first char, adds
  46.                 p("\n"+outputStr);                                                     // spaceStr, and adds last char
  47.             }
  48.             if(i == r-2){    // adds a line break for the last line
  49.                 pl("");
  50.             }
  51.         }
  52.         for(int i=r-1; i >= 0; i--){    // reverses string order
  53.             outputStr = lineStore.charAt(i) + " ";
  54.             p(outputStr);
  55.         }
  56.            
  57.     }
  58.     public static void p(String a) {    //print method
  59.         System.out.print(a);
  60.         return;
  61.     }  
  62.     public static void pl(String a) {   //println method
  63.         System.out.println(a);
  64.         return;
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement