isefire

BasicStringManipulation

Jul 21st, 2014
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.94 KB | None | 0 0
  1. /* StringManipulate
  2.  * <Description>
  3.  * Various string manipulation methods.
  4.  *
  5.  * <Usage>
  6.  * %javac StringManipulate.java
  7.  * %java String Manipulate Butties
  8.  * 1
  9.  * BUTTIES
  10.  * seittuB
  11.  *
  12.  * %java StringManipulate 1 Butties.jpg
  13.  * .jpg
  14.  *
  15.  * <References>
  16.  * CIS-201L Dr. Sloan Lab 8
  17.  * @author
  18.  * Dan Latham
  19.  * @version 0.0.1
  20.  *
  21.  */
  22. public class StringManipulate
  23. {
  24.     //Returns occurence of letter e in string
  25.     public static int occurrence(String string)
  26.     {
  27.         int count = 0;
  28.         int leng = string.length();
  29.         char[] str = new char[leng];
  30.         string.getChars(0,leng, str, 0);
  31.         for (int i = 0; i < leng; i+=1)
  32.         {
  33.             if (str[i] == 'e') count+=1;
  34.         }
  35.         return count;
  36.     }
  37.     //Returns string as STRING
  38.     public static String upper(String string)
  39.     {
  40.         return string.toUpperCase();
  41.     }
  42.     //Returns string as gnirts
  43.     public static String reverse(String string)
  44.     {
  45.         int leng = string.length();
  46.         int counter = 0;
  47.         char[] str = new char[leng];
  48.         char[] str_new = new char[leng];
  49.         string.getChars(0,leng, str, 0);
  50.         for (int i = leng-1; i >= 0; i-=1)
  51.         {
  52.             str_new[counter] = str[i];
  53.             counter+=1;
  54.         }
  55.         String basic = "";
  56.         for (int i = 0; i < leng; i +=1)
  57.         {
  58.             basic += str_new[i];
  59.         }
  60.         return basic;
  61.     }
  62.     //Returns file ext of file.jpg as .jpg
  63.     public static String filext(String string)
  64.     {
  65.         int leng = string.length();
  66.         char[] str = new char[leng];
  67.         String stringext = "";
  68.         string.getChars(0,leng, str, 0);
  69.         int i = leng-1;
  70.         while (i >= 0)
  71.         {
  72.             stringext += str[i];
  73.             //System.out.println(stringext);
  74.             if (str[i] == '.')
  75.             {
  76.                 i = -1;
  77.                 return reverse(stringext);
  78.             }
  79.             i -= 1;
  80.         }
  81.        
  82.         return "No File Extension Found in Filename";
  83.     }
  84.     public static void main(String[] args)
  85.     {
  86.         try
  87.         {
  88.             Integer.parseInt(args[0]);
  89.             String c = args[1];
  90.             System.out.println(filext(c));
  91.         }
  92.         catch (NumberFormatException j)
  93.         {
  94.             System.out.println(occurrence(args[0]));
  95.             System.out.println(upper(args[0]));
  96.             System.out.println(reverse(args[0]));
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment