001package algs12;
002
003import stdlib.*;
004public class XStrings {
005        public static boolean isPalindrome (String s) {
006                int N = s.length ();
007                for (int i = 0; i < N / 2; i++)
008                        if (s.charAt (i) != s.charAt (N - 1 - i)) return false;
009                return true;
010        }
011
012        public static boolean isSorted (String[] a) {
013                for (int i = 1; i < a.length; i++) {
014                        if (a[i - 1].compareTo (a[i]) > 0) return false;
015                }
016                return true;
017        }
018
019        public static void printBaseAndExtension (String s) {
020                // extract file name and extension from a command-line argument
021                int dot = s.lastIndexOf (".");
022                String base = s.substring (0, dot);
023                String extension = s.substring (dot + 1, s.length ());
024                StdOut.format ("base: %s\nextension: %s\n", base, extension);
025        }
026
027        public static void printLinesThatContain (String query) {
028                // print all lines in standard input that contain a string specified on the command line
029                while (!StdIn.isEmpty ()) {
030                        String s = StdIn.readLine ();
031                        if (s.contains (query)) StdOut.println (s);
032                }
033        }
034
035        public static void printSplit () {
036                // create an array of the strings on StdIn delimited by whitespace
037                String input = StdIn.readAll ();
038                String[] words = input.split ("\\s+");
039                for (int i = 0; i < words.length; i++) {
040                        StdOut.println (words[i]);
041                }
042        }
043
044        public static void main (String[] args) {
045                //printBaseAndExtension ("the/place/for/pig.dog.cat");
046                //StdIn.fromFile ("data/tale.txt");  printLinesThatContain ("children");
047                StdIn.fromFile ("data/tinyTale.txt"); printSplit ();
048        }
049}