Advertisement
therrontelford

Get Big Words

Feb 4th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. public class GetBigWords {
  2.  
  3.     // write a method getBigWords that will take one string and return an array of strings of
  4.     // that are of 5 letters or more.
  5.  
  6.     public static void main(String[] args) {
  7.         // provide a string
  8.         String name= "once upon a time there was a great castle";
  9.  
  10.         // call the method getBigWords
  11.         String[] a =getBigWords(name);
  12.         // print is only to check the method here
  13.         for (String e: a)
  14.             System.out.print(e+" ");
  15.     }
  16.     public static String[] getBigWords(String a){
  17.         create a large array of strings
  18.         String [] words= new String[100];
  19.        
  20.         // create a variable to store individual words
  21.         String word = "";
  22.  
  23.         // the counter is to keep up with the count of individual words over 5 letters long
  24.         int count=0;
  25.        
  26.         // a loop to check each character if it is a letter
  27.         for (int i=0; i<a.length(); i++){
  28.             char temp = a.charAt(i);
  29.             // use the Character class isLetter() method to check if true
  30.             if (Character.isLetter(temp)){
  31.    
  32.                 // if true add that character to word
  33.                 word+=a.charAt(i);
  34.  
  35.                     // if this is the last character of the string, add the word to the array if it is long enough
  36.                     if (i==a.length()-1 && word.length()>4)
  37.                         words[count] = word;
  38.             }
  39.             // if character is not a letter and less than 5 reset word variable to empty
  40.             else if (word.length()<5)
  41.                 word = "";
  42.                
  43.                 // if the character is not a letter and the word is long enough do these things
  44.                 else{
  45.                 words[count]= word;
  46.                 word="";
  47.                 count++;
  48.             }
  49.                
  50.                 }
  51.         // for aesthetic reasons only.  To return an array that is only as large as the number of words
  52.         String[] array = new String[count+1];
  53.         System.arraycopy(words,0,array,0,array.length);
  54.         return array;
  55.     }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement