JavaAlgorithms
Elementary and no so elementary Java algorithms
stringAlgorithms/Algorithms.java
Go to the documentation of this file.
00001 
00009 package stringAlgorithms;
00010 
00021 public class Algorithms {
00022 
00023     private Algorithms() {
00024     }
00025 
00034     public static String reverse(String str) {
00035         String newString = null;
00036         if (str != null) {
00037             char buf[] = str.toCharArray();
00038             int l = 0;
00039             int r = buf.length - 1;
00040             while (l < r) {
00041                 char t = buf[l];
00042                 buf[l] = buf[r];
00043                 buf[r] = t;
00044                 l++;
00045                 r--;
00046             }
00047             newString = new String(buf);
00048         }
00049         return newString;
00050     } // reverse
00051 
00062     public static boolean isSubstring(String sub, String str) {
00063         boolean match = false;
00064         if (sub != null && str != null) {
00065             int subLen = sub.length();
00066             int strLen = str.length();
00067             if (subLen <= strLen) {
00068                 int delta = strLen - subLen;
00069                 int i = 0;
00070                 do {
00071                     match = true;
00072                     int j = i;
00073                     for (int ix = 0; ix < subLen; ix++) {
00074                         if (sub.charAt(ix) != str.charAt(j)) {
00075                             match = false;
00076                             break;
00077                         }
00078                         j++;
00079                     } // for j
00080                     i++;
00081                 } while (i <= delta && (!match));
00082             }
00083         }
00084         return match;
00085     } // isSubstring
00086 
00087 }
 All Classes Namespaces Files Functions Variables