Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class StringOperations
- {
- /**
- * @param args
- */
- public static void main(String[] args)
- {
- // test compare appendFirstLetter method
- String[] words = { "Hi", "Ellwood", "Loves", "Likeable", "Objects" };
- String theNewWord = appendFirstLetter(words);
- System.out
- .println("The word created from the supplied array of words is: "
- + theNewWord);
- // test append Strings method
- theNewWord = appendStrings(words);
- System.out
- .println("The big word created from sticking the words in the word array together is: "
- + theNewWord);
- // test get words with S method
- /*
- String[] endsWithSTest = { "The", "animals", "ran", "over", "objects",
- "created", "by", "the masses" };
- */
- int amtWords = 1000;
- int wordLength = 30;
- String letter = "z";
- String[] endsWithSTest = {};
- endsWithSTest = new String[amtWords];
- for (int i = 0; i < amtWords; i++)
- {
- String word = generateGibberishString(wordLength);
- endsWithSTest[i] = word;
- }
- words = getWordsThatEndWithLetter(endsWithSTest, letter);
- for (int i = 0; i < words.length; i++)
- {
- System.out.println(words[i]);
- }
- System.out.println("Amount of gibberish words of length " + wordLength
- + " that end with " + letter + " is: "
- + getAmtWordsThatEndWithLetter(endsWithSTest, "s"));
- }
- public static String[] getWordsThatEndWithLetter(String[] words,
- String letter)
- {
- String l = letter;
- String res = "";
- System.out.println("The words that end with " + letter + " are: ");
- for (int i = 0; i < words.length; i++)
- {
- if (words[i].endsWith(l))
- {
- res = res.concat(words[i] + ":");
- }
- }
- String delims = "[:]";
- // set up String array to hold splitted input elements
- String[] wordsThatEndWithLetter = res.split(delims);
- return wordsThatEndWithLetter;
- }
- public static int getAmtWordsThatEndWithLetter(String[] words, String letter)
- {
- String l = letter;
- String res = "";
- int counter = 0;
- for (int i = 0; i < words.length; i++)
- {
- if (words[i].endsWith(l))
- {
- res = res.concat(words[i] + ":");
- counter++;
- }
- }
- return counter;
- }
- public static String appendStrings(String[] words)
- {
- String newWord = new String();
- for (int i = 0; i < words.length; i++)
- {
- newWord = newWord.concat(words[i]);
- }
- return newWord;
- }
- public static String appendFirstLetter(String[] words)
- {
- String newWord = new String();
- // Loop to iterate through our Array
- for (int i = 0; i < words.length; i++)
- {
- newWord = newWord.concat(Character.toString(words[i].charAt(0)));
- }
- return newWord;
- }
- public static String generateGibberishString(int wordLength)
- {
- String[] letters = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
- "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
- "w", "x", "y", "z", };
- int wl = wordLength;
- String randLetter = "";
- String randWord = "";
- Random gen = new Random();
- for (int i = 0; i < wl; i++)
- {
- randLetter = letters[gen.nextInt(26)];
- randWord = randWord.concat(randLetter);
- }
- return randWord;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment