From @aknight86, 3 Days ago, written in Java.
Embed
  1. /**
  2.  *String Notes and Method Examples
  3.  *Use this class for notes and homework
  4.  *
  5.  *@aknight86
  6.  *
  7.  *12/2/2019
  8.  *
  9.  */
  10.  
  11. public class StringPractice
  12. {
  13.     //Write your methods here
  14.    
  15.     /*
  16.      * Method returns the number of times a vowel occurs in a string
  17.      */
  18.     public static int vowelCount(String str)
  19.     {
  20.          int l = str.length();
  21.          int vCount = 0;
  22.          
  23.          for (int i = 0; i < l; i++)
  24.          {
  25.              if (str.charAt(i) == 'a' || str.charAt(i) == 'e' ||str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u')
  26.              {
  27.                  vCount++;
  28.                 }
  29.             }
  30.          return vCount;
  31.          
  32.     }
  33.    
  34.    
  35.     /*
  36.      * Method which takes in a string that is ten characters long (all digits)
  37.      * Assume the user in intelligent
  38.      * This method returns the string formatted as a pretty phone number
  39.      */
  40.     public static String prettyNum(String str)
  41.     {
  42.       String output = ("+1 (" + str.substring(0,3) + ")-" + str.substring(3,6) + "-" + str.substring(6));
  43.      
  44.       return output;
  45.     }
  46.    
  47.     /*
  48.      * This method provides a solution to question 7 on page 230.
  49.      * It reorders a given name so that it puts the first name followed by
  50.      * the last name
  51.      */
  52.    
  53.     public static String convertName(String name)
  54.     {
  55.       int x = name.indexOf(',');
  56.      
  57.       String converted = (name.substring(x+1) + " " + name.substring(0,x));
  58.      
  59.       return converted;
  60.     }
  61.    
  62.     /*
  63.      * This method provides the solution to question 8 on page 230.
  64.      * It takes in a string comprised of '0's and '1's and then switches the two characters
  65.      */
  66.     public static String switched(String seq)
  67.     {
  68.         int l = seq.length();
  69.        
  70.         String seq2 = "";
  71.        
  72.         for (int i = 0; i < l; i++)
  73.         {
  74.           if (seq.charAt(i) == '0')
  75.           {
  76.              seq2 += "1";
  77.             }
  78.            
  79.           else if (seq.charAt(i) == '1')
  80.           {
  81.              seq2 += "0";
  82.             }
  83.             else
  84.             {
  85.               seq2 += " ";  
  86.             }
  87.  
  88.         }
  89.        
  90.         return seq2;
  91.     }
  92.    
  93.    
  94.     /*
  95.      * This method prvides the solution to question 14 on page 231.
  96.      * It essentially checks if a statement is correctly written in HTML and then
  97.      * prints what it would actually be.
  98.      */
  99.     public static String removeTag(String str)
  100.     {
  101.       int l = str.length();
  102.      
  103.      
  104.       int x = str.indexOf('<');
  105.       int y = str.indexOf('>');
  106.      
  107.       int w = str.lastIndexOf('<');
  108.      
  109.       int q = str.lastIndexOf('>');
  110.      
  111.       int z = (str.substring(x, y)).length();
  112.      
  113.       if (str.startsWith("<") && str.endsWith(">"))
  114.       {
  115.           if (str.substring(1, z+1).equals(str.substring((q-z)+1)))
  116.          {
  117.           if (str.substring(w, w+2).equals("</"))
  118.           {
  119.               return str.substring(y+1, w);
  120.             }
  121.             else
  122.             {
  123.              return str;  
  124.             }
  125.           }
  126.           else
  127.           {
  128.              return str;
  129.             }
  130.         }
  131.         else
  132.         {
  133.             return str;
  134.         }
  135.      
  136.      
  137.     }
  138.    
  139.    
  140.     public static void main()
  141.     {
  142.       //Test for vowelCount
  143.       System.out.println("The number of vowels in the string in [The quick sly fox jumps over the lazy brown dog] is: " +
  144.       vowelCount("The quick sly fox jumps over the lazy brown dog"));
  145.      
  146.       System.out.println();
  147.      
  148.       System.out.println("7815456059 in fancy phone number format is: " + prettyNum("7815456059"));
  149.      
  150.       System.out.println();
  151.      
  152.       System.out.println("Knight, Alex  converted to the correct format is:" + convertName("Knight, Alex"));
  153.      
  154.       System.out.println();
  155.      
  156.       System.out.println("The 'negative' version of the string [01 10 11101010] is: " + switched("01 10 11101010"));
  157.      
  158.       System.out.println();
  159.      
  160.       System.out.println("The string [<abc>Mr. Blake and Mr. Senzi are besties</abc>] in HTML is: " +
  161.       removeTag("<abc>Mr. Blake and Mr. Senzi are besties</abc>"));
  162.     }
  163. }