Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************************************************
- Program: loops.java - This program performs word-in-a-box string mutations via loops.
- Author: Dave Aldrich
- Date: March 20, 2018
- ***************************************************************************************************************/
- import java.lang.*;
- import java.util.*;
- public class loops {
- public static void main(String[] args){
- Scanner scan = new Scanner(System.in);
- String inputStr = ""; // user input
- String lineStore = ""; // used for any arbituary length of string
- String outputStr = ""; // output string after mutations
- String spaceStr = " "; // whitespace string; length determined by spaceStore ***see the 2nd spaceStr doc***
- int spaceStore = 0; // stores the number of whitespaces in a mutated string, -2 because the last space
- // and character aren't needed
- p("Enter a string: "+inputStr);
- inputStr = scan.nextLine();
- lineStore = inputStr.toUpperCase();
- int r=lineStore.length(); // r stands for range
- for(int i=0; i < r; i++){
- outputStr = lineStore.charAt(i) + " ";
- p(outputStr);
- spaceStore++; // this will count how many spaces added
- }
- /*
- Makes another spaceStr obj but with repeated whitespaces using replace method.
- To clear up: the string is created using an array of null chars, but the nulls
- are replaced with the whitespace which was used to initialize spaceStr.
- */
- spaceStr = new String(new char[spaceStore*2-3]).replace("\0", spaceStr);
- for(int i=0; i < r-1; i++){
- if(i != 0){
- outputStr = lineStore.charAt(i)+spaceStr+lineStore.charAt(r - i-1); // takes first char, adds
- p("\n"+outputStr); // spaceStr, and adds last char
- }
- if(i == r-2){ // adds a line break for the last line
- pl("");
- }
- }
- for(int i=r-1; i >= 0; i--){ // reverses string order
- outputStr = lineStore.charAt(i) + " ";
- p(outputStr);
- }
- }
- public static void p(String a) { //print method
- System.out.print(a);
- return;
- }
- public static void pl(String a) { //println method
- System.out.println(a);
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement