-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringManips.java
More file actions
61 lines (53 loc) · 2.81 KB
/
StringManips.java
File metadata and controls
61 lines (53 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* Description: Originally this program shows: original phrase, it's length,
* middle index of the phrase, character in the middle. After changes in the
* program it's also will show 3 middle characters of the phrase.Program
* will ask user to enter hometown city and state. After program will print
* a string with name of state in upper case letters, name of the city in
* lower case letters, and again the state name in upper case.
* Lab# 3
*/
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; // to hold three middle characters
String city; //to hold name of the city
String state; //to hold name of the state
// 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);
// Changing swithcedPhrase.This statement changes all blank characters with an asterisk *
switchedPhrase = phrase.replace(" ", "*");
// using substring method to assign the substring consisting 3 middle characters of the phrase
middle3 = phrase.substring(middleIndex - 1, middleIndex + 2);
Scanner input = new Scanner(System.in);
// print information about the phrase
System.out.println();
System.out.println ("Original 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); //print swithedPhrase in the same println statement
System.out.println ("Three middle letters are: " + middle3); //print 3 middle characters
System.out.print ("Please, enter the name of your hometown: "); //asks user to enter city name
city = input.nextLine();
System.out.print ("Please, enter the name of your state: "); //asks user to enter state name
state = input.nextLine();
input.close();
//print formated name of the state and name of the city
System.out.println(state.toUpperCase() + " " + city.toLowerCase() + " " + state.toUpperCase());
System.out.println(state.toUpperCase() + city.toLowerCase() + state.toUpperCase());
System.out.println();
}
}