



|
|
CS1 Course Site
|
Programming Challenge Archive
Program Potpouri
String Thing
|
|
This program was featured in the lab devoted to our experiences with String manipulation, as well as giving us practice with writing operators.
JavaApplication --
String Thing
// General Information
// -------------------------------------------------------+
-----
// File: StringThingApp.java
// Type: java application file
// Date: November 1998
// Name: BLUE
// Line: Simple String Processing Application
// Application Description
// ------------------------------------------------
/*
Simple String processing application.
*/
// Required Packages
// -------------------------------------------------
import blue.io.*;
// Application Class
// ------------------------------------------------
class StringThingApp
{
// main method
static public void main (String args[])
{
// Create and print some strings which represent nam+
es
String singer = "Holliday, Billie";
String sculptor = "Claudel, Camille";
// Point A
String self = "Bremenour, James";
IO.println(singer);
IO.println(sculptor);
IO.println(self);
// Computer and print the lengths +
// Point B
int singerLength = singer.length();
int sculptorLength = sculptor.length();
int selfLength = self.length();
IO.println(); IO.println("Lengths ...");
IO.println(singerLength);
IO.println(sculptorLength);
IO.println(selfLength);
// Computer and print first names +
// Point C
String singerFirst = singer.substring(10);
String sculptorFirst = sculptor.substring(9);
String selfFirst = self.substring(11);
IO.println(); IO.println("First names ...");
IO.println(singerFirst);
IO.println(sculptorFirst);
IO.println(selfFirst);
// Computer and print last names +
// Point D
String singerLast = singer.substring(0,8);
String sculptorLast = sculptor.substring(0,7);
String selfLast = self.substring(0,9);
IO.println(); IO.println("Last names ...");
IO.println(singerLast);
IO.println(sculptorLast);
IO.println(selfLast);
// Computer and comma position within names +
// Point E
int singerPos = singer.indexOf(",");
int sculptorPos = sculptor.indexOf(",");
int selfPos = self.indexOf(",");
IO.println(); IO.println("Comma positions ...");
IO.println(singerPos);
IO.println(sculptorPos);
IO.println(selfPos);
// Computer and print first names +
// Point F1
singerFirst = firstName(singer);
sculptorFirst = firstName(sculptor);
selfFirst = firstName(self);
IO.println(); IO.println("First names ...");
IO.println(singerFirst);
IO.println(sculptorFirst);
IO.println(selfFirst);
// Computer and print last names +
// Point G1
singerLast = lastName(singer);
sculptorLast = lastName(sculptor);
selfLast = lastName(self);
IO.println(); IO.println("Last names ...");
IO.println(singerLast);
IO.println(sculptorLast);
IO.println(selfLast);
// Computer and print full names +
// Point H1
String singerFull = fullName(singer);
String sculptorFull = fullName(sculptor);
String selfFull = fullName(self);
IO.println(); IO.println("Full names ...");
IO.println(singerFull);
IO.println(sculptorFull);
IO.println(selfFull);
}
// Point F2
// operator-style method to compute first name
static private String firstName(String fName)
{
int comma = fName.indexOf(",");
String firstName = fName.substring(comma + 2);
return firstName;
}
// Point G2
// operator-style to compute last name
static private String lastName(String lName)
{
int comma = lName.indexOf(",");
String lastName = lName.substring(0, comma);
return lastName;
}
// Point H2
// operator-style to compute full name
static private String fullName(String flName)
{
int comma = flName.indexOf(",");
String fullName =flName.substring(comma + 2)
+ " " + flName.substring(0, comma);
return fullName;
}
}
// Demo
// --------------------------------------------
/*
$ java StringThingApp
Holliday, Billie
Claudel, Camille
Bremenour, James
Lengths ...
16
16
16
First names ...
Billie
Camille
James
Last names ...
Holliday
Claudel
Bremenour
Comma positions ...
8
7
9
First names ...
Billie
Camille
James
Last names ...
Holliday
Claudel
Bremenour
Full names ...
Billie Holliday
Camille Claudel
James Bremenour
$
*/
|
|
|