Skip to content

Instantly share code, notes, and snippets.

@mattcunningham
Last active January 10, 2017 18:00
Show Gist options
  • Save mattcunningham/06ba136cf22b20c48f0ed2ae7b7768f4 to your computer and use it in GitHub Desktop.
Save mattcunningham/06ba136cf22b20c48f0ed2ae7b7768f4 to your computer and use it in GitHub Desktop.
Chapter 12
public class StringOps {
private String[] words;
public StringOps(String[] wordStrings){
words = wordStrings;
}
public String stringHalf(boolean firstHalf) {
String finalString = "";
for (String word : words) {
if (firstHalf) {
for (int i = 0; i < (word.length()/2); i++) {
finalString = finalString + word.charAt(i);
}
} else {
for (int i = 0; i > (word.length()/2); i++) {
finalString = finalString + word.charAt(i);
}
}
}
return finalString;
}
public String stringLetterSelect(int letterIndex)
{
String finalString = "";
for (String word : words) {
if (word.length() - 1 >= letterIndex) {
finalString = finalString + word.charAt(letterIndex);
}
}
return finalString;
}
}
public class BlackAndWhite {
public String[][] blackOrWhite(String[][] colorArray) {
for (int i = 0; i < colorArray.length; i++) {
for (int j = 0; j < colorArray[i].length; j++) {
if (colorArray[i][j].equals("0")) {
colorArray[i][j] = "BLACK";
} else if (colorArray[i][j].equals("255")) {
colorArray[i][j] = "WHITE";
}
}
}
return colorArray;
}
}
import java.util.ArrayList;
public class StringSearch {
public int findString(String inputStr, String searchStr) {
return inputStr.indexOf(searchStr);
}
public void addString(String inputStr, int strIndex, ArrayList<String> wordsList) {
wordsList.add(inputStr.substring(strIndex, strIndex+3));
}
}
public class ReversePalindrome {
String reversedStr = "";
public String wordReverse(String originalStr) {
String reversed = "";
char[] originalChars = originalStr.toCharArray();
for (int i = originalChars.length - 1; i >= 0; i--) {
reversed += originalChars[i];
}
return reversed;
}
public boolean isPalindrome(String originalStr) {
if (originalStr.equals(wordReverse(originalStr))) {
return true;
} else return false;
}
}
import java.util.ArrayList;
public class SentenceSplitter {
public ArrayList<String> sentenceSplitter(String sentenceStr){
ArrayList<String> words = new ArrayList<String>();
for (String s : sentenceStr.split(" ")) {
words.add(s);
}
return words;
}
}
public class SentenceManager {
public double getVowels(String sentenceStr) {
double total = 0.0;
for (char c : sentenceStr.toCharArray()) {
if ("aeiou".indexOf(c) > -1) {
total += 1.0;
} else if (c == 'y') {
total += 0.5;
}
}
return total;
}
public int getConsonants(String sentenceStr) {
int total = 0;
for (char c : sentenceStr.toCharArray()) {
if ("aeiou ".indexOf(c) < 0) total += 1;
}
return total;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment