Created
December 13, 2017 11:37
-
-
Save JulianMiller/b8c9de49444f8393a5b8b7c919be44a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package firstsubroutines; | |
/** | |
* | |
* @author anonymous | |
*/ | |
public class Firstsubroutines { | |
// 1. Subroutine to find the reverse of a string | |
public static String getReversed(String str){ | |
String reverse =""; | |
int i; | |
str = str.toLowerCase(); | |
for (i = str.length() - 1; i >= 0; i--) { | |
if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z'){ | |
reverse = reverse + str.charAt(i); | |
} | |
} | |
return reverse; | |
} | |
// 2. Subroutine to strip string of whitespace and non-letter characters | |
public static String getStripped(String str){ | |
String stripped =""; | |
str = str.toLowerCase(); | |
for (int i=0; i<= str.length() - 1; i++) { | |
if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z') | |
stripped = stripped + str.charAt(i); | |
} | |
return stripped; | |
} | |
// 3. Checks for palindromes | |
public static void main(String[] args) { | |
String str="", getReversed = "", getStripped = ""; | |
TextIO.putln("Enter your phrase"); | |
str = TextIO.getln(); | |
String stripped = getStripped(str); | |
String reversed = getReversed(str); | |
TextIO.putln("stripped:" + getStripped(str)); | |
TextIO.putln("reversed:" + getReversed(str)); | |
if (stripped.equals(reversed)) | |
TextIO.putln("This IS a palindrome"); | |
else | |
TextIO.putln("This is NOT a palindrome"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment