Skip to content

Instantly share code, notes, and snippets.

@joeyv
Created October 21, 2013 17:34
Show Gist options
  • Save joeyv/7087753 to your computer and use it in GitHub Desktop.
Save joeyv/7087753 to your computer and use it in GitHub Desktop.
idk lol
import java.util.Scanner;
public class StringManips
{
public static void main (String[] args)
{
String phrase = new String ("This is a String test.");
int phraseLength; // number of characters in the phrase String
int middleIndex; // index of the middle character in the String
String firstHalf; // first half of the phrase String
String secondHalf; // second half of the phrase String
String switchedPhrase; // a new phrase with original halves switched
String middle3;
String city, state, newCityState;
// declare scanner
Scanner scan = new Scanner(System.in);
// get input for city/state
city = scan.next();
state = scan.next();
// create new string with city/state
newCityState = state.toUpperCase() +"" + city.toLowerCase() + "" + state.toUpperCase();
// compute the length and middle index of the phrase
phraseLength = phrase.length();
middleIndex = phraseLength / 2;
// get the substring for each half of the phrase
firstHalf = phrase.substring(0,middleIndex);
secondHalf = phrase.substring(middleIndex, phraseLength);
middle3 = phrase.substring(middleIndex-1, middleIndex+2);
// concatenate the firstHalf at the end of the secondHalf
switchedPhrase = secondHalf.concat(firstHalf);
// new switched phrase
switchedPhrase = switchedPhrase.replace(' ', '*');
// print information about the phrase
System.out.println("\nOriginal phrase: " + phrase);
System.out.println("Length of the phrase: " + phraseLength +
" characters");
System.out.println("Index of the middle: " + middleIndex);
System.out.println("Character at the middle index: " +
phrase.charAt(middleIndex));
System.out.println("Switched phrase: " + switchedPhrase);
System.out.println("Middle3 phrase: " + middle3);
System.out.println("New switched phrase: " + switchedPhrase);
System.out.println("City/State: " + newCityState);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment