123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- /**
- * <https://y.st./>
- * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org./licenses/>.
- **/
- $xhtml = array(
- 'title' => 'firstsubroutines.java',
- 'subtitle' => 'Written in <span title="Programming 1">CS 1102</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalised on 2017-05-03',
- 'copyright year' => '2017',
- 'body' => <<<END
- <h2>Source code:</h2>
- <blockquote>
- <pre><code>/*
- * Copyright (C) 2017 Alex Yst
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package firstsubroutines;
- import java.util.Scanner;
- /**
- * Alex Yst's CS 1102 (Programming 1) Unit 4 submission
- *
- * This class was programmed to fill the requirements on the Unit 4 programming
- * assignment in University of the People's CS 1102 (Programming 1) course.
- *
- * @author Alex Yst
- */
- public class firstsubroutines {
- /**
- * This method reverses the string passed to it as an argument, and thus the
- * name of this method.
- *
- * @param str The string to reverse
- * @return The reversed string
- */
- public static String reverse(String str) {
- String reverse = "";
- for (int i = str.length() - 1; i >= 0; i--) {
- reverse = reverse + str.charAt(i);
- }
- return reverse;
- }
- /**
- * This method removes all non-letters and converts all letters to lower case.
- *
- * In computer science, the conversion of strings to a unified for like this,
- * in which all equivilant strings are converted to an identical form, is
- * called normalization, and thus the name of this method.
- *
- * @param str A string to be normalized
- * @return The normalized string
- */
- public static String normalize(String str) {
- str = str.toLowerCase();
- int end = str.length() -1;
- String normalized = "";
- for (int i = 0; i <= end; i++) {
- char ch = str.charAt(i);
- if(ch >= 'a' && ch <= 'z') {
- normalized = normalized + ch;
- }
- }
- return normalized;
- }
- /**
- * This is just the main method. In Java, the main() method is called. Code
- * isn't simply included outside all method definitions when it needs to be
- * run.
- *
- * @param arguments This parameter is unused.
- */
- public static void main(String[] arguments) {
- Boolean loop = true;
- Scanner scanner = new Scanner(System.in);
- String forward;
- String reverse;
- String compare;
- System.out.println("Please enter a string to be checked, or enter an empty string to abort:");
- String input = scanner.nextLine();
- while(!input.equals("")) {
- forward = normalize(input);
- reverse = reverse(forward);
- System.out.println("stripped: "+forward);
- System.out.println("reversed: "+reverse);
- if(forward.equals(reverse)) {
- compare = "IS";
- } else {
- compare = "is NOT";
- }
- System.out.println("This "+compare+" a palindrome.");
- System.out.println("Please enter a string to be checked, or enter an empty string to abort:");
- input = scanner.nextLine();
- }
- }
- }</code></pre>
- </blockquote>
- <h2>Output:</h2>
- <blockquote>
- <pre>run:
- Please enter a string to be checked, or enter an empty string to abort:
- Hello World!
- stripped: helloworld
- reversed: dlrowolleh
- This is NOT a palindrome.
- Please enter a string to be checked, or enter an empty string to abort:
- Campus motto: Bottoms up, Mac!
- stripped: campusmottobottomsupmac
- reversed: campusmottobottomsupmac
- This IS a palindrome.
- Please enter a string to be checked, or enter an empty string to abort:
- BUILD SUCCESSFUL (total time: 21 seconds)</pre>
- </blockquote>
- END
- );
|