clanecollege

String Operations - solution

May 16th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class StringOperations
  4. {
  5.  
  6.     /**
  7.      * @param args
  8.      */
  9.     public static void main(String[] args)
  10.     {
  11.         // test compare appendFirstLetter method
  12.         String[] words = { "Hi", "Ellwood", "Loves", "Likeable", "Objects" };
  13.         String theNewWord = appendFirstLetter(words);
  14.         System.out
  15.                 .println("The word created from the supplied array of words is: "
  16.                         + theNewWord);
  17.  
  18.         // test append Strings method
  19.         theNewWord = appendStrings(words);
  20.         System.out
  21.                 .println("The big word created from sticking the words in the word array together is: "
  22.                         + theNewWord);
  23.  
  24.         // test get words with S method
  25.         /*
  26.         String[] endsWithSTest = { "The", "animals", "ran", "over", "objects",
  27.                 "created", "by", "the masses" };
  28.         */
  29.         int amtWords = 1000;
  30.         int wordLength = 30;
  31.         String letter = "z";
  32.         String[] endsWithSTest = {};
  33.         endsWithSTest = new String[amtWords];
  34.         for (int i = 0; i < amtWords; i++)
  35.         {
  36.             String word = generateGibberishString(wordLength);
  37.             endsWithSTest[i] = word;
  38.         }
  39.  
  40.         words = getWordsThatEndWithLetter(endsWithSTest, letter);
  41.         for (int i = 0; i < words.length; i++)
  42.         {
  43.             System.out.println(words[i]);
  44.         }
  45.         System.out.println("Amount of gibberish words of length " + wordLength
  46.                 + " that end with " + letter + " is: "
  47.                 + getAmtWordsThatEndWithLetter(endsWithSTest, "s"));
  48.  
  49.     }
  50.  
  51.     public static String[] getWordsThatEndWithLetter(String[] words,
  52.             String letter)
  53.     {
  54.         String l = letter;
  55.         String res = "";
  56.         System.out.println("The words that end with " + letter + " are: ");
  57.         for (int i = 0; i < words.length; i++)
  58.         {
  59.             if (words[i].endsWith(l))
  60.             {
  61.                 res = res.concat(words[i] + ":");
  62.             }
  63.  
  64.         }
  65.         String delims = "[:]";
  66.         // set up String array to hold splitted input elements
  67.         String[] wordsThatEndWithLetter = res.split(delims);
  68.  
  69.         return wordsThatEndWithLetter;
  70.     }
  71.  
  72.     public static int getAmtWordsThatEndWithLetter(String[] words, String letter)
  73.     {
  74.         String l = letter;
  75.         String res = "";
  76.         int counter = 0;
  77.         for (int i = 0; i < words.length; i++)
  78.         {
  79.             if (words[i].endsWith(l))
  80.             {
  81.                 res = res.concat(words[i] + ":");
  82.                 counter++;
  83.             }
  84.  
  85.         }
  86.         return counter;
  87.     }
  88.  
  89.     public static String appendStrings(String[] words)
  90.     {
  91.         String newWord = new String();
  92.         for (int i = 0; i < words.length; i++)
  93.         {
  94.             newWord = newWord.concat(words[i]);
  95.         }
  96.         return newWord;
  97.     }
  98.  
  99.     public static String appendFirstLetter(String[] words)
  100.     {
  101.         String newWord = new String();
  102.         // Loop to iterate through our Array
  103.         for (int i = 0; i < words.length; i++)
  104.         {
  105.             newWord = newWord.concat(Character.toString(words[i].charAt(0)));
  106.         }
  107.         return newWord;
  108.     }
  109.  
  110.     public static String generateGibberishString(int wordLength)
  111.     {
  112.         String[] letters = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
  113.                 "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  114.                 "w", "x", "y", "z", };
  115.         int wl = wordLength;
  116.         String randLetter = "";
  117.         String randWord = "";
  118.         Random gen = new Random();
  119.         for (int i = 0; i < wl; i++)
  120.         {
  121.             randLetter = letters[gen.nextInt(26)];
  122.             randWord = randWord.concat(randLetter);
  123.         }
  124.         return randWord;
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment