Java Reference
In-Depth Information
pol
yhedr o n
pol
yhedr a l
System.out.println("o:"+(int)'o');
System.out.println("a:"+(int)'a');
int diff='o'-'a'; // implicit casting char->int
System.out.println(diff);
We get:
o:111
a:97
14
Now consider the case of one string being the substring of another one:
String a="polyhedral",b="polyhedralization";
System.out.println(a.compareTo(b));
System.out.println(a.length()-b.length());
We find both times
7. This is the difference between the length of string a
and the length of string b .
We can implement our own static function for performing the lexicographic
order on strings as follows:
Program 5.10 Implementation of the lexicographic order on strings
static int LexicographicOrder ( String p, String q)
int i=0;
while (i < p. length () && i < q. length () )
{ if (p. charAt( i )==q . charAt( i ) )
i ++;
else
return p.charAt(i)
q.charAt(i);
return p.length() q. length () ;
}
As a final example of the use of lexicographic order of strings in programs, let us
play again with the string array argument of the main function ( public static
void main(String[ ] args) ). The following program finds among the string
arguments of the main function the lexicographically smallest string:
Program 5.11 Reporting the lexicographically minimum string of the
command line arguments
class ParsingArgument {
public static void main( String [ ]
args )
{ String minimum=args [0];
for ( int i=1; i < args . length ; i++)
 
Search WWH ::




Custom Search